android显示任何应用程序顶部弹出窗口的通知

jus*_*ser 43 android

使用下面的代码,我的通知只会添加到通知栏中,不会显示弹出样式消息,就像您在另一个应用程序中收到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_HIGHNotificationCompat.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_HIGHNotification.PRIORITY_MAX在API级别26中弃用.请NotificationCompat改用.

是更多信息:-)

  • 对于那些遵循这些说明并尝试它们的人.我观察到,并且确实耗费了我几个小时,在更改代码中的重要性时,卸载应用程序并从头开始安装它是非常有帮助的.否则,您可能看不到通知窗口小部件的重要性和外观方面的任何更改. (13认同)
  • 现在不推荐使用`Notification.PRIORITY_HIGH`和`Notification.PRIORITY_MAX`。 (2认同)
  • 优先级还不够。如我的回答所述,必须有铃声或振动。您的示例中的通知可能在“ setDefaults()”中添加了铃声和振动,但您没有提及。 (2认同)
  • @Herman 你是上帝,这是可以接受的答案,但不起作用。我红色你的评论并尝试它现在工作。 (2认同)

And*_*rs8 32

您必须将通知优先级设置为Notification.PRIORITY_HIGHNotification.PRIORITY_MAX.我也必须这样做.setDefaults(Notification.DEFAULT_ALL).


Mar*_*ius 5

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)


jua*_*aza 5

API 级别 26 以下:发送消息时在 Builder 中设置 Notification.PRIORITY_HIGH 或 Notification.PRIORITY_MAX。

API 级别 26 或以上:

将通道优先级设置为高

重要的:

一旦通道具有既定优先级,就无法更改。如果是LOW不会显示弹窗,除非你重新安装APP。

  • 谢谢,我可以确认重新安装后最终会弹出通知。确保渠道的重要性永远不会改变 (4认同)
  • 谢谢,这部分“一旦通道建立了优先级,就无法更改。如果它处于低优先级,则不会显示弹出窗口,除非您重新安装应用程序”是问题的关键。在代码中设置新的优先级后,我修复了将我的 chanel id 更改为新的 id 的问题。 (2认同)