【问题标题】:BroadcastReceiver does not get data from notification buttonBroadcastReceiver 没有从通知按钮获取数据
【发布时间】:2018-04-04 05:07:44
【问题描述】:

我正在尝试使用删除按钮发出通知,通知存储在 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 获得的值始终为 null ...我一直在尝试寻找解决方案,但没有结果。

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 时,它什么也做不了。

【问题讨论】:

    标签: android notifications broadcastreceiver notification-action


    【解决方案1】:

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

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

    您也可以使用FLAG_ONE_SHOT。如果它满足您的用例。您可以通过PendingIntent中使用的不同标志

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多