【后台弹窗】应用跳转直通车--通知栏通知跳转后台应用

 

【关键字】

鸿蒙、弹窗、通知栏、后台应用

 

【问题描述】

近期华为手机更新为HarmonyOS后发现应用在后台时,通知栏推送消息无法跳转应用界面。

参考开发者联盟中的以下文档【FAQ】【弹窗问题】关于后台弹窗问题-华为开发者论坛 | 华为开发者联盟 (huawei.com)

原因是华为新增了后台弹窗权限,根据提供的Android官网文档连接需要修改通知为显示通知,以下把个人的踩坑过程提供给各位参考。

 

【原因与实现步骤】

参考Android官网文档连接需要修改通知为显示通知,并使用了全屏Intent但是发现并没有生效,仍然无法跳转到后台Activity中。注意到官网代码中有以下注释:

// Use a full-screen intent only for the highest-priority alerts where you
// have an associated activity that you would like to launch after the user
// interacts with the notification. Also, if your app targets Android 10
// or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
// order for the platform to invoke this notification

不生效的原因是必须要在Manifest中添加USE_FULL_SCREEN_INTENT权限,这个权限是普通权限并不需要申请使用。

因此具体的修改流程如下:

第一步:

在AndroidManifest.xml中增加USE_FULL_SCREEN_INTENT权限:

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

第二步:

参考官网创建全屏Intent通知,最后使用NotificationManager进行通知即可

  Intent fullScreenIntent = new Intent(this, CallActivity.class);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
            fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Incoming call")
        .setContentText("(919) 555-1234")
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setCategory(NotificationCompat.CATEGORY_CALL)

        // Use a full-screen intent only for the highest-priority alerts where you
        // have an associated activity that you would like to launch after the user
        // interacts with the notification. Also, if your app targets Android 10
        // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
        // order for the platform to invoke this notification.
        .setFullScreenIntent(fullScreenPendingIntent, true);

    Notification incomingCallNotification = notificationBuilder.build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(100, incomingCallNotification);

 欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

posted @ 2023-03-01 14:16  华为开发者论坛  阅读(592)  评论(0编辑  收藏  举报