【问题标题】:Full screen intent not starting the activity but do show a notification on android 10全屏意图未启动活动,但确实在 android 10 上显示通知
【发布时间】:2020-01-12 01:54:44
【问题描述】:

我尝试使用下一个代码为广播接收器启动活动

 Intent i = new Intent(context, AlarmNotification.class);
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...

                NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                if (mgr.getNotificationChannel(CHANNEL_WHATEVER)==null) {
                    mgr.createNotificationChannel(new NotificationChannel(CHANNEL_WHATEVER,
                            "Whatever", NotificationManager.IMPORTANCE_HIGH));
                }

                mgr.notify(NOTIFY_ID, buildNormal(context, i).build());

            }

private NotificationCompat.Builder buildNormal(Context context, Intent intent) {

    NotificationCompat.Builder b=
            new NotificationCompat.Builder(context, CHANNEL_WHATEVER);

    b.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(TEXT)
            .setContentText(TEXT)
            .setFullScreenIntent(buildPendingIntent(context, intent), true);

    return(b);

}

private PendingIntent buildPendingIntent(Context context, Intent intent) {

    return(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}

一开始,一切都很好。但是如果我进入应用设置,关闭CHANNEL_WHATEVER的通知通道,然后再打开。稍后当我调用 NotificationManager.notify 时,它会在通知抽屉中显示通知,但不会启动活动。如果我删除该应用程序并重新安装,它会再次正常工作。这是我应该报告的android 10的错误,还是我可以做些什么?

【问题讨论】:

  • 禁用然后启用该频道后,该频道上的所有其他选项是否与“设置”中的相同?如果您由于禁用/启用周期而在该频道上丢失 IMPORTANCE_HIGH,则您的症状匹配。
  • @CommonsWare 如何判断我是否丢失了 IMPORTANCE_HIGH?如果是这样,我该怎么办?
  • “如果是这样,我该怎么办?” - 没有,除了可能提交错误报告。 “我怎么知道我是否丢失了 IMPORTANCE_HIGH?” -- 以编程方式,当mgr.getNotificationChannel(CHANNEL_WHATEVER) 不是null 时,您应该能够查看它的结果。致电getImportance() 并记录您看到的值。
  • @CommonsWare 你说得对。重要性从 IMPORTANCE_HIGH 变为 IMPORTANCE_LOW。我会举报的。
  • @SimpleUXApps 我也面临同样的问题。你是如何设法让它工作的?请发布或接受答案。

标签: android android-intent broadcastreceiver android-pendingintent android-10.0


【解决方案1】:

在 Android 10 中,我们需要为 USE_FULL_SCREEN_INTENT 添加权限

全屏意图的权限更改

  • 面向 Android 10 或更高版本并使用全屏意图通知的应用必须在其应用的清单文件中请求 USE_FULL_SCREEN_INTENT 权限。

  • 这是一个正常的权限,所以系统会自动授予请求的应用程序。

确保您已在清单文件中添加权限

示例代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nilu.demo">

    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【讨论】:

  • 感谢您的回答。我已经在使用这个权限了。
  • 您好,我正在使用此权限,但无法显示全屏意图?请帮忙
  • 添加了这个权限,用户仍然报告他们只看到通知
  • @Dmitrijs 你有没有找到任何解决方案,因为我现在面临同样的问题:/
  • @Tarun 你有没有找到任何解决方案,因为我现在面临同样的问题:/
【解决方案2】:

即使您使用全屏 Intent,Android 也不会允许显示活动:

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

【讨论】:

  • 那么我们应该如何在 Android 10 上启动警报呢?据我了解,全屏意图是唯一的选择。
  • 你是对的,这是唯一的选择,但正如我所说,系统可以决定不显示活动,而只显示平视通知
  • 当屏幕打开时,我的应用程序关闭,然后系统显示一个抬头通知,之后,我屏幕关闭并看到全屏活动,为什么?这是一个错误吗?
  • @FereshtehNaji 没有。例如,当我们接到电话时,如果我们正在使用电话,则该电话不会劫持整个屏幕。不使用电话时,通话将占据全屏。这是相似的。
  • 我也面临着类似的问题。当您说使用警报时,是对通知或任何其他活动的一些修改吗?如有报警请提供参考。
【解决方案3】:

请阅读我关于如何在 OS 10 上以全屏意图启动 Activity 的文章。该文章还解释了如何显示提示通知和处理操作按钮点击。

https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4

【讨论】:

  • 如果设备被锁定,全屏意图是否有效?
  • @Bojke 请参考我的文章medium.com/@dcostalloyd90/…
  • 在您的文章的锁定屏幕上没有看到任何与通知显示相关的内容
  • 不起作用。我有全屏意图权限和通知,与您在文章中的完全一样。我收到了广播,但活动没有开始。
【解决方案4】:

不确定是否已回答。除了前面提到的权限......我发现使这项工作的唯一方法是使用“NotificationManager.IMPORTANCE_HIGH”频道和通知。 设置为 HIGH 后,如果屏幕关闭,将打开 fullScreenIntent。稍后更改频道似乎需要卸载并重新安装。

在发送通知之前设置通知通道 (see also):

private static void createNotificationChannel(Context context) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = **"YOUR CHANNEL"**;
            String description = "YOUR CHANNEL DESCRIPTION";
            int importance = **NotificationManager.IMPORTANCE_HIGH**;
            NotificationChannel channel = new NotificationChannel( "YOUR CHANNEL ID", name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

我设置了两个意图,不清楚是否必须同时设置:

createNotificationChannel((context));
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, **"YOUR CHANNEL**");
    builder.setContentIntent(pendingIntent);
    builder.setFullScreenIntent(pendingIntent,true);

【讨论】:

    【解决方案5】:

    如果您想在锁定屏幕上显示您自己的活动,您需要在 Manifest 中启用 showOnLockScreen

    <activity android:name="NewActivity" android:showOnLockScreen="true" /> 
    

    并在您的活动类中设置标志 showWhenLocked:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);
            setTurnScreenOn(true);
        }
    }
    

    最后用你的活动类创建一个 fullScreenIntent 并将它附加到通知中

    Intent fullScreenIntent = new Intent(applicationContext, NewActivity.class);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(applicationContext, 0,
        fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(applicationContext, channel)
        .setSmallIcon(R.drawable.ic_small)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setContentIntent(pendingIntent)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setCategory(NotificationCompat.CATEGORY_ALARM)
        .setFullScreenIntent(fullScreenPendingIntent, true)
        .setAutoCancel(true);
    

    【讨论】:

      【解决方案6】:

      您应该在显示通知之前唤醒 Android 屏幕:

      fun asquireWake() {
          val mPowerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
          val mWakeLock: PowerManager.WakeLock = mPowerManager.newWakeLock(
              PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
              "YourApp:Whatever"
          )
          mWakeLock.acquire()
      }
      

      【讨论】:

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