請參見下面的代碼來處理不同的數據處理的多個通知:在上面的代碼
private void sendNotification(Bundle extras)
{
String detail = extras.getString("detail");
String title = extras.getString("title");
if((title==null || title.trim().length()==0) && (detail==null || detail.trim().length()==0))
{
//title=getString(R.string.app_name);
return;
}
NOTIFICATION_ID = (int) (System.currentTimeMillis()/1000L);
Intent notificationIntent = new Intent(this, ContainerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String data = extras.getString("data");
notificationIntent.putExtra("data",data);
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle = bigTextStyle.bigText(detail);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(detail)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.app_logo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_logo))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setVibrate(new long[] { 0, 100, 200, 300 })
.setPriority(NotificationCompat.PRIORITY_MAX)
.setStyle(bigTextStyle);
mBuilder.setDeleteIntent(getDeleteIntent(NOTIFICATION_ID));
Set<String> pnIdsSet = ((MyAccountApplication)getApplication()).getPrefs().getStringSet("PUSH_IDS", null);
if(pnIdsSet==null)
{
pnIdsSet=new HashSet<String>();
}
pnIdsSet.add(""+NOTIFICATION_ID);
((MyAccountApplication)getApplication()).getPrefs().edit().putStringSet("PUSH_IDS", pnIdsSet).commit();
mBuilder.setContentIntent(pendingIntent);
Notification n = mBuilder.build();
n.flags |= Notification.FLAG_SHOW_LIGHTS;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.defaults = Notification.DEFAULT_ALL;
n.when = System.currentTimeMillis();
mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, n);
}
private PendingIntent getDeleteIntent(int pnId)
{
Intent intent = new Intent(this, NotificationBroadcastReceiver.class);
intent.setAction("notification_cancelled");
intent.putExtra("PN_ID", ""+pnId);
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
我處理通知刪除的意圖也看到每個通知具有的PendingIntent不同的通知ID。 FLAG_CANCEL_CURRENT標誌。
這個問題如何從http://stackoverflow.com/questions/36055555/how-to-handle-gcm-notifications-when-different-gcm-notifications-receive? – Michael
你能告訴我解決方案 – Sanjeev
不,你不應該多次發表相同的問題。請參閱http://stackoverflow.com/help/no-one-answers – Michael