【问题标题】:API 17 - Android notification launches activity without being clickedAPI 17 - Android 通知无需点击即可启动活动
【发布时间】:2017-06-24 03:11:58
【问题描述】:

我在 API 17 上发布了一条通知,并且在未点击通知的情况下触发了待处理的意图。

这是我在发布通知时使用的代码,代码的哪一部分触发了这种行为,我该如何解决?

 public static void notifyIncomingMessage(Context context, ChatMessage message, String name) throws JSONException {
        NotificationManager mNotificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(context, ChatMessageInterceptor.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(context, GcmIntentService.NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        String text = messageText(message, context);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_small_notification)
                        .setContentTitle(name == null ? context.getString(R.string.app_name) : name)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(text))
                        .setContentText(text)
                        .setLargeIcon(getBitmapIcon(context))
                        .setContentIntent(contentIntent)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setAutoCancel(true)
                        .setFullScreenIntent(contentIntent, true);
        mNotificationManager.notify(GcmIntentService.NOTIFICATION_ID, mBuilder.build());
    }

【问题讨论】:

  • 是什么触发了意图?只是阅读通知会启动活动?或者,刚刚收到通知就是再次打开活动?
  • @GuilhermeP 当通知发布时意图触发

标签: android notifications


【解决方案1】:

主要原因是由于setFullScreenIntent()方法,正如android的文档告诉[developer.android.com][1] 所以不要使用

    mBuilder.setFullScreenIntent(contentIntent, true);

而只是使用

    mBuilder.setPriority(Notification.PRIORITY_HIGH)
    .setDefaults(Notification.DEFAULT_ALL);

这将有助于默认显示提示通知。 即使对于 setDefaults(),Notification.DEFAULT_ALL 也不是必需的,但很好用。

这很清楚,对我有用。

【讨论】:

    【解决方案2】:

    显然它一定是fullScreenIntent。我将其删除并仅添加到 API >= 19 中。我仍然需要在 KitKat 设备上进行测试。

    if (Utils.hasLollipop())
        mBuilder.setFullScreenIntent(contentIntent, true);
    

    没有注意文档,但在这里。

    意图启动而不是将通知发布到状态 酒吧。仅用于要求极高优先级的通知 用户的即时关注,例如来电或 用户已明确设置为特定时间的闹钟。如果 此设施用于其他用途,请给用户一个 关闭它并使用正常通知的选项,因为这可以是 极具破坏性。

    在某些平台上,系统 UI 可能会选择显示提示 通知,而不是启动此意图,而用户 使用设备。

    我实际上只想要提醒通知,但如何检测拥有或没有提醒的设备完全是另一个问题。

    【讨论】:

      猜你喜欢
      • 2015-08-21
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 2020-05-07
      相关资源
      最近更新 更多