使用下面的代码,我的通知只会添加到通知栏中,不会显示弹出样式消息,就像您在另一个应用程序中收到whatsapp消息一样.是什么让这种情况发生在通知中?
private void sendNotification(int distance, ViewObject viewObject) {
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("path", viewObject.getPath());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(Integer.parseInt(viewObject.getRefId()), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(String.format(getString(R.string.notification), viewObject.getTitle()));
bigText.setBigContentTitle(getString(R.string.hello));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(bigText);
builder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
}
Run Code Online (Sandbox Code Playgroud)
Red*_*ne1 100
如果你想这样使用Heads-up Notifications:

您必须更改Notification优先级或NotificationChannel重要性.
通知优先级,由setPriority().优先级决定了Android 7.1及更低版本通知的侵入程度.(对于Android 8.0及更高版本,您必须改为设置频道重要性)
在Android 7.1(API级别25)及更低版本:
NotificationCompat.PRIORITY_HIGH或NotificationCompat.PRIORITY_MAX.setDefaults(Notification.DEFAULT_ALL)Android 8.0(API级别26)及更高版本:
NotificationManager.IMPORTANCE_HIGH通知:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(bigText)
.setPriority(NotificationCompat.PRIORITY_HIGH) // or NotificationCompat.PRIORITY_MAX
Run Code Online (Sandbox Code Playgroud)
通知渠道:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.notification_channel_name)
val descriptionText = getString(R.string.notification_channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
Run Code Online (Sandbox Code Playgroud)
如果您想进一步定制频道的默认通知的行为,你可以调用的方法,如
enableLights(),setLightColor()和setVibrationPattern()上NotificationChannel.但请记住,一旦创建了频道,就无法更改这些设置,用户可以最终控制这些行为是否处于活动状态.
可能触发抬头通知的条件示例包括:
- 用户的活动处于全屏模式(应用程序使用fullScreenIntent).
- 该通知具有高优先级,并在运行Android 7.1(API级别25)及更低级别的设备上使用铃声或振动.
- 通知渠道对运行Android 8.0(API级别26)及更高版本的设备具有高度重要性.
优先:
Notification.PRIORITY_HIGH并Notification.PRIORITY_MAX在API级别26中弃用.请NotificationCompat改用.
这是更多信息:-)
And*_*rs8 32
您必须将通知优先级设置为Notification.PRIORITY_HIGH或Notification.PRIORITY_MAX.我也必须这样做.setDefaults(Notification.DEFAULT_ALL).
Android 系统正在决定通知是否显示为Heads-up notification。有多种情况可能会触发提醒通知:
可能触发平视通知的条件示例包括:
- 用户的活动处于全屏模式(应用程序使用 fullScreenIntent)。
- 该通知具有高优先级,并在运行 Android 7.1(API 级别 25)及更低版本的设备上使用铃声或振动。
- 通知通道对于运行 Android 8.0(API 级别 26)及更高版本的设备非常重要。
来源:https ://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
因此,如果您运行的是 Android 7.1 及更低版本,请务必添加铃声或振动以及高优先级。对于 Android 8.0 及更高版本,将优先级更改为高重要性。
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
// ...
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_HIGH); // or HIGH_IMPORTANCE for Android 8.0
Run Code Online (Sandbox Code Playgroud)
API 级别 26 以下:发送消息时在 Builder 中设置 Notification.PRIORITY_HIGH 或 Notification.PRIORITY_MAX。
API 级别 26 或以上:
将通道优先级设置为高
重要的:
一旦通道具有既定优先级,就无法更改。如果是LOW不会显示弹窗,除非你重新安装APP。
| 归档时间: |
|
| 查看次数: |
56952 次 |
| 最近记录: |