首页 > 解决方案 > 全屏意图未启动活动,但确实在 android 10 上显示通知

问题描述

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

 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 的错误,还是我可以做些什么?

标签: androidandroid-intentbroadcastreceiverandroid-pendingintentandroid-10.0

解决方案


在 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>

推荐阅读