[英]How maintain GCM push message in Android, its replaced by new one
我的Android應用程序收到帶有一些文本消息的推送通知。如果我點擊某個推送,它將使用最新的推送消息(意圖消息)將我重定向到所需的活動,但我想通過相應的推送消息顯示所需的活動。 例如,如果我收到10個推送通知,然后點按第3個通知,則我的代碼會將我重定向到具有第10個推送通知的消息的指定活動,但我想顯示第3個意圖推送通知的消息。
我知道PendingIntent.FLAG_UPDATE_CURRENT替換了意圖消息,如何用相應的消息而不是最后一條消息重定向?
我嘗試了以下方法:
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。 此答案將使之很清楚。
您應該更改“不同消息”的pendingIntend的內容。 這是PendingIntent文檔的摘錄。 “ PendingIntent本身只是對系統維護的令牌的引用,該令牌描述了用於檢索它的原始數據。這意味着,即使其擁有的應用程序的進程被殺死,PendingIntent本身也可以從已給定的其他進程中繼續使用如果創建應用程序以后重新檢索相同類型的PendingIntent(相同的操作,相同的Intent操作,數據,類別和組件以及相同的標志),則它將收到代表相同令牌的PendingIntent(如果該令牌仍然有效),因此可以調用cancel()將其刪除。”
用簡單的話說...嘗試為每個pendingIntent傳遞不同的id(類似currentEpochTime)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.