我觉得这应该很简单,但我似乎无法在手机屏幕上显示通知-它仅显示在顶部的状态栏中。
作为我想要做的事的示例,这是当您收到消息时Facebook Messenger在屏幕上显示的方式。
[每当我发送通知时,它所做的就是在状态栏中显示小图标-即使我将优先级设置为PRIORITY_MAX。我需要做其他设置以使其在屏幕上显示,而不仅仅是状态栏吗?
通知显示代码:
PendingIntent contentIntent = PendingIntent.getActivity(context, nextId++, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle(title)
.setContentText(description)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_notification)
.setLargeIcon(largeIcon)
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL);
if (android.os.Build.VERSION.SDK_INT >= 21) {
builder.setColor(context.getResources().getColor(R.color.orange_500))
.setVisibility(Notification.VISIBILITY_PUBLIC);
}
Notification notification = builder.build();
notificationManager.notify(id, notification);
考虑所有因素,在NotificationCompat.Builder
上使用Notification.Builder
是一个[[really好主意,更不用说手动创建Notification
了。它为您提供了良好的向后兼容性以及平滑的降级功能(一直返回到API级别4,否则称为“ gadzooks,它很旧”)。 AFAIK,当与NotificationManagerCompat
配合使用时,这是使Android Wear某些功能正常运行的唯一方法。而且,在这种情况下,它似乎更喜欢较新的Android 5.0+功能。
setPriority(NotificationCompat.PRIORITY_HIGH)
一起使用的NotificationCompat.Builder
上的NotificationManagerCompat
会在Android 5.0+上向您发出提示通知。NotificationManager.IMPORTANCE_HIGH
。 这将配置发布到此频道的视觉干扰通知的状态,如果设置为“高”,它将允许其窥视。如果您将此设置为NotificationManager.IMPORTANCE_DEFAULT
,则不会!
在配置通知频道时请注意,一旦代码在您的设备上运行,您将无法再次更改。因此,如果您需要更改重要性,则需要卸载该应用程序,然后重新运行,您应该会在屏幕上看到该通知!