通知通过老意图附加 [英] Notification passes old Intent Extras

查看:138
本文介绍了通知通过老意图附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这个code创建一个BroadcastReceiver里的通知:

i am creating a notification inside a BroadcastReceiver via this code:

String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
        int icon = R.drawable.ic_stat_notification;
        CharSequence tickerText = "New Notification";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        long[] vibrate = {0,100,200,200,200,200};
        notification.vibrate = vibrate;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        CharSequence contentTitle = "Title";
        CharSequence contentText = "Text";
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        notificationIntent.putExtra(Global.INTENT_EXTRA_FOO_ID, foo_id);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        int mynotification_id = 1;

        mNotificationManager.notify(mynotification_id, notification);

当我点击的通知,它打开NotificationActivity和活动里面,我可以检索的意向捆绑的foo_id(例如:1)

When I click on the notification, it opens the NotificationActivity and inside the Activity i can retrieve the foo_id from the Intent-Bundle (e.g. 1)

不过,如果另一个通知被触发,我再次点击它,该活动仍收到意向捆绑旧的值(1)。我试图清除捆绑明确(),但我收到同样的效果。我认为某事是错误的与我的code ..

However if another notification is triggered and i click on it again, the activity still receives the "old" value (1) from the Intent-Bundle. I've tried to clear the bundle with clear(), but am receiving the same effect. I think sth is wrong with my code..

推荐答案

您发送相同的请求,code为您的待处理INTENS。 更改此设置:

You are sending the same request code for your pending intens. Change this:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

要:

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);

如果您发送相同的PARAMS不会创建

意图。它们被重复使用。

intents are not created if you send the same params. They are reused.

这篇关于通知通过老意图附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆