转载请注明出处 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()); }