我正在使用FCM
来获取新的通知,我可以毫无问题地显示该通知,但是当我打开应用程序时,我的所有通知都消失了
这是我用于创建新通知的代码
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext,mContext.getString(R.string.default_notification_channel_id));
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(mContext.getString(R.string.default_notification_channel_id), name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notification);
单击通知时,它会打开一个包含额外数据的活动我的意图
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", "wwwwwwww");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
现在,当我单击任何通知时,它会打开主活动,其中包含主活动所依赖的一些额外值。
但是当我单击通知并启动活动时,所有其他通知都消失了。那么,我如何防止这种情况并仅隐藏单击的通知,以便用户可以看到其他通知
不太
确定它是否有效,但您可以尝试将请求代码设置为唯一而不是 0。
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
将 0 替换为以下内容
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,(int) System.currentTimeMillis(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
试试这个
**.setPriority(NotificationManager.IMPORTANCE_HIGH)**
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(ICON)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notification);