【问题标题】:Android notification setFullScreenIntent doesn't automatically launch the activityAndroid 通知 setFullScreenIntent 不会自动启动活动
【发布时间】:2020-11-19 22:55:07
【问题描述】:

我想通过通知自动启动全屏活动,但 setFullScreenIntent 有问题。我遵循了这个:https://developer.android.com/training/notify-user/time-sensitive 和互联网上的线程,但这对我来说不起作用。 我的通知出现了,但我必须按下它才能启动活动。

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = this.getSystemService(NotificationManager.class);

            if (notificationManager != null && notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "channel_name", NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription("channel_description");
                notificationManager.createNotificationChannel(channel);
            }            
        }

        Intent fullScreenIntent = new Intent(this, ResultActivity.class);
        fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                        .setSmallIcon(R.drawable.notification_icon)
                        .setContentTitle("Full Screen Notification")
                        .setContentText("Notification test text")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setFullScreenIntent(fullScreenPendingIntent, true);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(12345, notificationBuilder.build()); 

这是我的清单:

  <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
  <activity
        android:name=".ResultActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" /> -->
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
  </activity>

谢谢!

【问题讨论】:

    标签: android android-intent android-activity android-notifications


    【解决方案1】:

    我猜当你锁定手机时,全屏 Intent 会正确显示,对吧?

    您所看到的称为heads-up notifications。如文档所述,这是高重要性通知的正常行为,它们会在设备解锁时出现。

    你应该怎么做?

    保持锁定屏幕的全屏意图,但也为您的通知提供自定义布局,这将在更好的上下文 UI 中呈现您的功能,而不是默认通知。


    示例:

    闹钟应用(在 Pixel 手机上)有以下触发闹钟的情况:

    • 屏幕解锁时启动提醒通知
    • 屏幕锁定时的全屏意图。

    【讨论】:

    • 感谢您的回答!我更了解它是如何工作的。事实上,我会检查锁定屏幕以切换到不同的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多