简体   繁体   English

如何在Android中维护GCM推送消息,将其替换为新消息

[英]How maintain GCM push message in Android, its replaced by new one

My Android application receives push notification with some text messages.If I tap a push it redirects me to desired activity with latest push message (intent message) but I want to show my desired activity with corresponding push messages. 我的Android应用程序收到带有一些文本消息的推送通知。如果我点击某个推送,它将使用最新的推送消息(意图消息)将我重定向到所需的活动,但我想通过相应的推送消息显示所需的活动。 For example If I receives 10 push notifications and I tap 3rd notification, my code redirects me to the specified activity with 10th push notification's message, but I want to show 3rd intent push notification's message. 例如,如果我收到10个推送通知,然后点按第3个通知,则我的代码会将我重定向到具有第10个推送通知的消息的指定活动,但我想显示第3个意图推送通知的消息。

I know PendingIntent.FLAG_UPDATE_CURRENT replace the intent message, how can I redirect with corresponding message instead last message? 我知道PendingIntent.FLAG_UPDATE_CURRENT替换了意图消息,如何用相应的消息而不是最后一条消息重定向?

I have tried the following: 我尝试了以下方法:

Intent intent = new Intent(this, TestActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("uid", uid);

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        getApplicationContext());
Notification notification = mBuilder
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker(textMsg)
        .setWhen(0)
        .setAutoCancel(true)
        .setContentTitle(textMsg)
        .setStyle(
                new NotificationCompat.BigTextStyle().bigText(textMsg))
        .setContentIntent(resultPendingIntent).setContentText(textMsg)
        .build();

NotificationManager notificationManager = (NotificationManager) getApplicationContext()
        .getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) System.currentTimeMillis(),
        notification);

请改用FLAG_ONE_SHOT然后将getActivity的第二个参数更改为0。 答案将使之很清楚。

You should Change the pendingIntend's content for Different Messages. 您应该更改“不同消息”的pendingIntend的内容。 Here is the snippet from PendingIntent document. 这是PendingIntent文档的摘录。 "A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it." “ PendingIntent本身只是对系统维护的令牌的引用,该令牌描述了用于检索它的原始数据。这意味着,即使其拥有的应用程序的进程被杀死,PendingIntent本身也可以从已给定的其他进程中继续使用如果创建应用程序以后重新检索相同类型的PendingIntent(相同的操作,相同的Intent操作,数据,类别和组件以及相同的标志),则它将收到代表相同令牌的PendingIntent(如果该令牌仍然有效),因此可以调用cancel()将其删除。”

In simple words...try passing different id(Something like currentEpochTime) for each pendingIntent. 用简单的话说...尝试为每个pendingIntent传递不同的id(类似currentEpochTime)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM