【问题标题】:android all notification disappeared when open the appandroid打开应用时所有通知都消失了
【发布时间】:2019-03-29 04:20:00
【问题描述】:

我正在使用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
                );

现在,当我单击任何通知时,它会打开 MainActivity,其中包含一些 Main Activity 所依赖的额外值。

但是当我点击一个通知并启动活动时,所有其他通知都消失了。那么,我怎样才能防止这种情况并仅隐藏点击的通知,以便用户可以看到其他通知

【问题讨论】:

  • 我认为设置 Autocancel false 可以工作
  • 我也试过了:/
  • .setPriority(Notification.PRIORITY_HIGH)怎么样
  • 为通知提供不同的 id 可能会解决您的问题。也将相同的 id 传递给待处理的意图。
  • @primo int id = (int) System.currentTimeMillis(); notificationManager.notify(id, notification); 已经这样做了

标签: java android firebase-cloud-messaging android-notifications


【解决方案1】:

不太确定它是否会起作用,但您可以尝试将请求代码设置为唯一而不是 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);

【讨论】:

  • 不工作 .. 当只打开 MainActivity 其他活动工作而不消失通知时会发生此问题
【解决方案2】:

试试这个

**.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);

【讨论】:

  • 为什么启动 MainActivity 时所有通知都消失了
  • 因为您必须设置通知的优先级。
  • 我在从 MainActivity 离开时显示通知覆盖并返回所有通知消失之前尝试过
  • 您的代码中没有新内容只有优先级**.setPriority(NotificationManager.IMPORTANCE_HIGH)**,如果您阅读了上面对我的问题的评论,我之前尝试过
  • 好的,然后从您返回的任何特定活动然后通知清除或从您返回的任何活动然后通知清除?
猜你喜欢
  • 2015-07-25
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 2014-10-29
  • 2015-04-05
  • 1970-01-01
相关资源
最近更新 更多