【问题标题】:android show notification with a popup on top of any applicationandroid 在任何应用程序顶部显示带有弹出窗口的通知
【发布时间】:2015-07-09 01:54:23
【问题描述】:

使用下面的代码,我的通知只会添加到通知栏,不会显示弹出式消息,就像您在另一个应用程序中时会收到 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());
}

【问题讨论】:

    标签: android


    【解决方案1】:

    如果你想像这样使用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
    

    通知渠道:

    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)
    }
    

    重要

    如果您想进一步自定义频道的默认通知行为,可以调用enableLights()setLightColor()@等方法987654336@NotificationChannel 上。但请记住,一旦您创建了通道,就无法更改这些设置,并且用户可以最终控制这些行为是否处于活动状态。其他选项是卸载并再次安装应用程序。 Read more


    可能触发提醒通知的条件示例包括:

    • 用户的活动处于全屏模式(应用程序使用 全屏意图)。
    • 通知具有高优先级并使用 运行 Android 7.1(API 级别 25)的设备上的铃声或振动 及更低。
    • 通知通道在设备上具有很高的重要性 运行 Android 8.0(API 级别 26)及更高版本。

    优先级:

    Notification.PRIORITY_HIGHNotification.PRIORITY_MAX 在 API 级别 26 中已弃用。请改用 NotificationCompat

    Here 是更多信息 :-)

    【讨论】:

    • 如果我希望我的活动按原样触摸事件并且不关闭此通知而不是可能的?
    • 现在 Notification.PRIORITY_HIGHNotification.PRIORITY_MAX 已弃用。
    • 优先级不够。如我的回答中所述,必须有铃声或振动。您示例中的通知可能添加了带有setDefaults() 的铃声和振动,但您没有提及。
    • 对于那些遵循此说明并尝试它们的人。我观察到,这确实花费了我几个小时,当更改代码的重要性时,卸载应用程序并从头开始安装它会很有帮助。否则,您可能看不到通知小部件的重要性和外观方面的任何变化。
    • @Herman 你是神人,这是公认的答案,但不起作用。我把你的评论加红了,现在试了一下。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      API 级别 26 或以上:

      设置通道优先级高

      重要:

      频道一旦确定了优先级,就无法更改。如果是LOW就不会弹出,除非你重新安装APP。

      【讨论】:

      • 谢谢,我可以确认重新安装后终于弹出通知了。确保渠道的重要性永远不会改变
      • 谢谢,这部分“一旦频道具有确定的优先级,它就不能更改。如果它处于LOW将不会显示弹出窗口,除非你重新安装APP”是问题的关键。在代码中设置新的优先级后,我修复了将我的 chanel id 更改为新的。
      【解决方案4】:

      Android 系统正在决定是否将通知显示为提示通知。有几种情况可能触发提醒通知:

      可能触发提醒通知的条件示例包括:

      • 用户的 Activity 处于全屏模式(应用使用 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多