android - 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());
如果有人知道这个问题,请回答。
谢谢阅读。
解决方案
我遇到了这个问题,问题是我有两个具有相同 ID 的不同通知。
一个与服务相关联,因此当我取消绑定服务时,它会终止通知,但由于两者具有相同的 ID,因此会终止两个通知。
调用 notify 方法时更改通知 ID 可解决问题:
notificationManager?.Notify(2, notification); //Previously I had "1" which matched another notification that had 1 as ID
我花了大约 4 个小时才找到答案,但最终在我的场景中它与通知设置无关。
推荐阅读
- android - 布局 XML 中字符串格式的数据绑定
- presto - 使用 Facebook Presto SQL Parser 生成 SQL 语句
- algorithm - CSES范围查询问题:薪资查询
- css - 当我在本地运行良好时将文件上传到 github 时,Css 未正确应用。可能是什么原因?
- python - 在 Python 中,有没有办法找到两个日期之间的天数?
- tableau-api - 按画面中的最大日期分组
- javascript - 谷歌日历客户端javascript缺少刷新令牌
- python - 将字符串转换为二维数组python
- excel - Excel VBA - 使用嵌套 if 和 vlookups 引用 2 个组合框值
- java - 无模式 JSON 到 Apache Beam“行”类型?