【问题标题】:Android push disappears after a few secondsAndroid推送几秒钟后消失
【发布时间】:2018-11-14 01:01:41
【问题描述】:

我的测试手机是使用 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());

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

感谢阅读。

【问题讨论】:

    标签: android push-notification


    【解决方案1】:

    对我来说它工作得很好(除了这里的单引号 .setContentTitle('My App') 和这里的 .setContentText('test'))!

    可能存在一些系统问题 - 或者此行为可能是由您应用的其他部分引起的。

    顺便提一下,NotificationCompat.Builder 已被弃用;现在它需要一个额外的字符串channelId。

    【讨论】:

    • 完美运行意味着,nougat s6 状态栏还有推送文本吗?还是只是警报?推送通知对我来说也做得很好,但问题是在警报状态栏后仍有应用程序推送但状态栏中没有推送。而且我们必须修复 targetSDK 版本,所以可能几天或几周前我们会更改 notificationCompat。谢谢你。
    • 我无法完全理解您的问题 - 对我来说,它按预期工作。通知会在屏幕上显示几秒钟,然后它会出现在状态栏中并一直保持在那里,直到单击或关闭。对于不同的行为,也许你应该改变Notification.PRIORITY。或者你可以分享一些截图?
    • 基本上,通知后,状态栏中有推送内容(应用图标,应用名称,推送消息)。对?但是我的应用推送内容在通知后消失了。在通知之前的片刻,在状态栏中有推送内容,但在通知之后消失了..
    【解决方案2】:

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

    一个与服务绑定,所以当我解除绑定服务时,它会终止通知,但由于两者的 ID 相同,它会终止两个通知。

    在调用 notify 方法时更改通知 ID 修复了问题:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多