【发布时间】:2017-03-13 11:15:53
【问题描述】:
我有以下代码,我想在用户点击通知时检索一些数据。但我不知道如何处理它。 我知道有一种方法可以将活动作为参数传递,但取决于通知的内容,我应该打开不同的活动。
这是愿望工作流程
我发布通知(带有键值数据)-> 用户点击通知-> 广播接收器(检索键值数据)-> 活动
问题是我的 BroadcastReceiver 没有接收数据。
Intent notificationIntent = new Intent(NOTIFICATION_SELECTED_ACTION);
notificationIntent.putExtra("mykey","myvalue");
PendingIntent intent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
context.getApplicationContext().registerReceiver(receiverOpen, new IntentFilter(NOTIFICATION_SELECTED_ACTION));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
...
mBuilder.setContentIntent(intent);
//I publish the notification in the notification bar
notification = mBuilder.build();
mNotificationManager.notify(id_push, notification);
private final BroadcastReceiver receiverOpen = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context.getApplicationContext(),MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Value is always null ffs
String value = intent.getStringExtra("mykey");
count = 0; // Do what you want here
notificationList.clear();
editor.clear();
editor.commit();
context.startActivity(i);
}
};
【问题讨论】:
-
当您按下通知时。如果您有来自通知的价值,那么应该没有任何问题。在mainActivity中根据值可以开启不同的不同activity。
标签: android android-notifications