Android推送通知横幅未在某些设备中显示
我尝试使用Android推送通知横幅未在某些设备中显示,android,push-notification,Android,Push Notification,我尝试使用NotificationCompat推送通知: NotificationCompat.Builder b = new NotificationCompat.Builder(this); b.setAutoCancel(true) .setDefaults(NotificationCompat.DEFAULT_ALL) .setWhen(System.currentTimeMillis
NotificationCompat
推送通知:
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(this.getResources().
getIdentifier("ic_launcher", "mipmap", this.getPackageName()))
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
this.getResources().
getIdentifier("ic_launcher", "mipmap", this.getPackageName())))
.setTicker("God Reacts")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle(data.get("lineOne"))
.setContentText(data.get("lineTwo"))
.setContentInfo("Spread the message !");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this,getMainActivityClass()),
PendingIntent.FLAG_UPDATE_CURRENT);
b.setContentIntent(contentIntent);
NotificationManager nm = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, b.build());
但在少数设备(三星、MI等)中,不会显示通知横幅。
通知以声音和振动的方式在操作托盘中滑动
但在少数设备中,当应用程序关闭/背景/前景时,它会完美地显示出来。它弹出的设备正确地使用了棉花糖。这是由于特定的操作系统造成的吗?还是与设备相关的问题?我还需要补充什么?由于电池优化,一些牛轧糖版本的设备可能会出现问题。
例如三星Galaxy S8和S8+、Oneplus、xaiomi等,但在Android版本的牛轧糖中, 您可以尝试一次,因为您必须检查一次设置 第1步:转到设置> 第2步:搜索“电池优化”> 第3步:从这里点击“未优化的应用程序”并切换到“所有应用程序”。 第4步:搜索你的应用程序(你没有收到通知) 第5步:点击你的应用程序名称并将其设置为未优化,以便它可以接收通知 或 步骤1:进入设置>应用程序 步骤2:单击右上角的:菜单,选择>[特殊访问] 步骤3:选择>优化电池使用> 第4步:点击屏幕顶部的下拉菜单,上面写着:“应用程序未优化[v]”->更改为[所有应用程序]
第5步:选择您的应用程序并将其更改为“未优化”。在中文设备中,如MI或OPPO等。系统关闭了非白名单应用程序的通知服务,因此对此你无能为力。只需请求他们将你的应用程序列入白名单(一般情况下发生这种情况的可能性很小),或者以某种方式让你的应用程序始终保留在内存中,比如说,运行一些24*7的空白后台服务,第一次尝试使用最小通知属性。尝试以下代码,在我的应用程序中运行良好。如果有效,则可以通过启用通知的一个属性来调试代码
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_notification)
.setContentTitle("Touchrooms Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
当通知使用了图标或图像并且无法加载该图标/图像时,我遇到了类似的问题。您可以通过将设置图标的行从参考资料更改为通过
resu id
加载图标来轻松测试它,如下所示:
b.setSmallIcon(R.drawable.new_mail);
b.setLargeIcon(R.drawable.my_icon);
您当前加载这些图标的方法可能会在这些设备上失败,因此,通知会有声音和振动,但不会显示任何可见内容。某些设备的设置存在问题
需要从设置->应用->通知打开“
浮动通知”
。仅在某些特殊情况下,某些设备可能会安装通知清理应用,它们可以清理您的通知。我以前遇到过这个问题,它完全让我发疯。也许你可以检查一下。@Spark.Bao我认为情况并非如此。我在2台真实设备上检查了它,但没有通知清理应用程序。我不明白这些问题发生的原因。你检查了这个:?@josealfonsomora是的,我检查了。谢谢你的回答。但问题也存在于旧版本中(例如kitkat)好吧!!将尝试其他方法并在有效时更新您。我的应用程序确实运行了一些后台服务。您可以为您的解决方案添加更多详细信息吗?@arul没有更多内容了。