安卓Notification 推送多條只顯示一條問題
阿新 • • 發佈:2018-11-02
轉載請註明出處 http://blog.csdn.net/qq_31715429/article/details/50978587
本文出自:猴菇先生的部落格
(1).推送多條手機全能接收,但是隻顯示一條通知,後一條會頂掉前一條,最後只顯示一條最新的notification
Intent intent = new Intent(context, MainActivity.class); intent.putExtra(PUSH_CLASSID,pushBean.getClassId()); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle(); bigTextStyle.setBigContentTitle(title); bigTextStyle.bigText(content); Notification nc = new NotificationCompat.Builder(context) .setDefaults(Notification.DEFAULT_ALL) .setTicker(content) .setContentTitle(title) .setContentText(content) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(pendingintent) .setStyle(bigTextStyle) .build(); NotificationManager nm = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE); nm.notify(id,nc);
最後的原因是nm.notify(id, nc)中的id是一個固定值,需要定義一個變數
public int id = 0;
然後修改nm.notify(id++, nc);這樣就會顯示多條推送。
(2). 還有一個問題,就是雖然會顯示多條推送,但是點選最早的前幾條條推送,如果彈窗的話,顯示的總是最新的那條資料資訊
關鍵在這:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- 這個方法的第二個引數應該是對應不同的推送的id,把這個值改成不同的值,以對應不同的推送資訊,就可以顯示不同的資訊。本來想的是每次推送生成一個唯一值,比如System.currentTimeMillis()或者UUID.randomUUID(),但是考慮到PendingIntent.getActivity方法第二個引數需要int值,怕出現型別轉換異常,後來改用SharedPreference在本地記錄一個int值,每次接收推送時get一下,再++一下,接收完再put一下。
private void sendNotification(Bundle bundle, GetFirebaseMessageNotificationResponse notification) { ++NOTIFY_ID; if (bundle != null) { AnalyticsUtil.getInstance().facebookEventLogger(AppConstant.AnalyticEvent.NOTIFICATION_MESSAGE_SHOW, "type", bundle.getString("type")); } Intent intent = new Intent(this, WelcomeActivity.class); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(AppConstant.ActivityBundle.NOTIFICATION_LAUNCH_TO_WELCOME, true); intent.putExtra(AppConstant.Common.IntentKey.FROM_NOTIFICATION, AppConstant.Common.FROM_NOTIFICATION_KEY); if (notification != null) { intent.putExtra(AppConstant.Common.IntentKey.FROM_NOTIFICATION_CONTENT, notification.getContent()); } PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFY_ID /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon_logo_white). setContentTitle(notification.getTitle()) .setContentText(notification.getContent()).setAutoCancel(true) .setSound(defaultSoundUri).setContentIntent(pendingIntent); Bitmap bm = BitmapFactory.decodeResource(CCApplication.getInstance().getResources(), R.mipmap.monkey_icon); notificationBuilder.setLargeIcon(bm); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFY_ID /* ID of notification */, notificationBuilder.build()); }