代码之家  ›  专栏  ›  技术社区  ›  Zolo

BroadcastReceiver未从通知按钮获取数据

  •  1
  • Zolo  · 技术社区  · 6 年前

    我试图用remove按钮发出通知,通知存储在SQLite DB中,按下后,我想按id删除“记录”。首先,我得到一个通知,将其存储到DB,存储唯一id,并将其传递给创建通知的方法。方法很好。

        public static String CUSTOM_ACTION = "com.example.app.Services.REMOVE";
        Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
        snoozeIntent.setAction(CUSTOM_ACTION);
        snoozeIntent.putExtra("NOT_WORKING", String.valueOf(id));
        PendingIntent snoozePendingIntent =
                PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);
    
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        String channelId = getString(R.string.default_notification_channel_id);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.ic_stat_bell)
                .setContentTitle("Loggly")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.ic_delete_forever_black_24dp, "Remove", snoozePendingIntent);
    

    然后我有一个广播接收器,用于处理来自通知的数据。下面是它在我的清单文件中的外观。

        <receiver android:name=".MyBroadcastReceiver"  android:exported="false">
            <intent-filter>
                <action android:name="com.example.app.Services.REMOVE"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    

    这是广播接收机,我从extras得到的值总是空的。。。我一直在努力寻找解决方案,但没有结果。

    public class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
            String id;
    
            if (extras != null) {
                id = extras.getString("NOT_WORKING");
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
                if (notificationManager != null) {
                    notificationManager.cancel(0);
                }
    
                Intent inten = new Intent(context, RemoveService.class);
                inten.putExtra("NOT_WORKING", id);
                context.startService(inten);
            }
        }
    }
    

    最后,我启动了intent服务,它应该从数据库中删除“记录”,但当广播接收器没有收到id时,它就无能为力了。

    1 回复  |  直到 5 年前
        1
  •  4
  •   Gautam    6 年前

    我刚刚在我这边创建了一个示例通知,问题是您创建PendingContent的方式。只需将适当的标志添加到pendingIntent参数,它就会正常工作。

    PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(this, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    您可以使用 FLAG_ONE_SHOT 也如果它满足您的用例。您可以浏览不同的标志,这些标志可用于 PendingIntent