活动无法在意向接收时更新

发布于 2024-11-14 14:54:00 字数 1280 浏览 2 评论 0原文

在我发送两个具有不同内容(例如 Content1 和 Content2)的通知后,以下代码给出了相同的内容。生成的活动始终仅显示 Content2。可能是什么原因?

public void onReceive(Context context, Intent intent) {
    abortBroadcast();

    mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.icon;
    CharSequence tickerText = intent.getStringExtra("NOTIFICATION_TITLE");
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    CharSequence contentTitle = intent.getStringExtra("NOTIFICATION_TITLE");
    CharSequence contentText = intent.getStringExtra("NOTIFICATION_DETAILS");
    Intent notificationIntent = new Intent(context,CustomActivity.class);
    notificationIntent.putExtra("TITLE", contentTitle);
    notificationIntent.putExtra("DETAILS", contentText);
    //notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(notifUUID.hashCode(), notification);


}

The following code gives the same content after I send two notifications with different content say Content1 and Content2. The resulting activity always shows only Content2. What could be the reason for it?

public void onReceive(Context context, Intent intent) {
    abortBroadcast();

    mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.icon;
    CharSequence tickerText = intent.getStringExtra("NOTIFICATION_TITLE");
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    CharSequence contentTitle = intent.getStringExtra("NOTIFICATION_TITLE");
    CharSequence contentText = intent.getStringExtra("NOTIFICATION_DETAILS");
    Intent notificationIntent = new Intent(context,CustomActivity.class);
    notificationIntent.putExtra("TITLE", contentTitle);
    notificationIntent.putExtra("DETAILS", contentText);
    //notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(notifUUID.hashCode(), notification);


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末 2024-11-21 14:54:00

得到答案了!修复很简单 - 只需添加:
notificationIntent.setAction(String.valueOf(notifUUID.hashCode()));

设置为意图操作的任何唯一值(时间戳也可以工作)都可以工作。

Got the answer! the fix was simple - just added:
notificationIntent.setAction(String.valueOf(notifUUID.hashCode()));

Any unique value (timestamp would have worked too) set as the intent action would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文