首页 > 解决方案 > Android推送几秒钟后消失

问题描述

我的测试手机是使用 Android 7.0 Nougat 的三星 Galaxy S6。

在其他应用程序中,Android 推送通知保留在状态栏中,但在我的应用程序中,推送通知在收到通知后消失。

问题是从状态栏中消失的通知。与通知警报无关。推送通知做得很好。响铃后,状态栏没有推送。

有谁知道这种情况?

这是代码:

NotificationCompat.Builder builder = new 
NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.icon_noti)
.setContentTitle('My App')
.setContentText('test')
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true);

PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 
0, new Intent(getApplicationContext(), MainActivity.class), 
PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent);

NotificationManager mNotificationManager = (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(0, builder.build());

如果有人知道这个问题,请回答。

谢谢阅读。

标签: androidpush-notification

解决方案


我遇到了这个问题,问题是我有两个具有相同 ID 的不同通知。

一个与服务相关联,因此当我取消绑定服务时,它会终止通知,但由于两者具有相同的 ID,因此会终止两个通知。

调用 notify 方法时更改通知 ID 可解决问题:

notificationManager?.Notify(2, notification); //Previously I had "1" which matched another notification that had 1 as ID

我花了大约 4 个小时才找到答案,但最终在我的场景中它与通知设置无关。


推荐阅读