关于Android Q上点击通知无法跳转的问题

在Android Q 即api 29上,可能会出现使用PendingIntent点击通知无法跳转的问题,但是你找了半天找不到问题,举个例子,app自动更新兼容android Q的时候就会遇到这个问题,下载完安装,当app处于前台的时候没有任何问题,在后台的时候就会出现问题,因为google在Android Q 上禁止后台启动activity了,所以当你在gradle中设置:

targetSdkVersion 29

后就会出现app在后台的时候无法启动系统的安装程序了,于是查阅资料人家会告诉你这样写:
app兼容Android Q启动安装程序兼容代码:

    public static void installApk(Context mContext, File apkFile) {
        if (!apkFile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
            Uri apkFileUri = FileProvider.getUriForFile(mContext,
                    mContext.getPackageName() + ".fileprovider", apkFile);
            i.setDataAndType(apkFileUri, "application/vnd.android.package-archive");
            mContext.startActivity(i);
        } else {
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setDataAndType(Uri.parse("file://" + apkFile.toString()),
                    "application/vnd.android.package-archive");
            mContext.startActivity(i);
        }
    }

首先你会发现这样写前台安装没问题,但是后台安装就会有问题了,然后利用点击通知启动安装程序的方式兼容android Q后台启动activity:

/**
 * Created by 方舟 on 2017/10/13.
 * 更新通知
 */
public class UpdateNotificationUtil extends ContextWrapper {

    private Context mContext;
    private static NotificationManager mManager;
    private NotificationCompat.Builder mBuilder;
    private static UpdateNotificationUtil updateNotificationUtil;

    public static UpdateNotificationUtil getInstance() {
        if (updateNotificationUtil != null && mManager != null) {
            return updateNotificationUtil;
        }
        synchronized (ApplicationHelper.getInstance()) {
            if (updateNotificationUtil == null || mManager == null) {
                updateNotificationUtil = new UpdateNotificationUtil(ApplicationHelper.getInstance());
            }
        }
        return updateNotificationUtil;
    }

    private UpdateNotificationUtil(Context context) {
        super(context);
        this.mContext = context;
        mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(ConstantsHelper.NOTIFY_CHANNEL_ID,
                ConstantsHelper.NOTIFY_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        mManager.createNotificationChannel(channel);
    }

    public void sendNotificationFullScreen(String title, String content, File apkFile) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
            Uri apkFileUri = FileProvider.getUriForFile(ApplicationHelper.getInstance(),
                    ApplicationHelper.getInstance().getPackageName() + ".fileprovider", apkFile);
            i.setDataAndType(apkFileUri, "application/vnd.android.package-archive");
            PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(ApplicationHelper.getInstance(),
                    1, i, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, ConstantsHelper.NOTIFY_CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(title)
                            .setTicker(content)
                            .setContentText(content)
                            .setAutoCancel(true)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .setCategory(Notification.CATEGORY_CALL)
                            .setFullScreenIntent(fullScreenPendingIntent, true);
            Notification notification = notificationBuilder.build();
            mManager.notify(1, notification);
        }
    }

    public void clearAllNotification() {
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.cancelAll();
        }
    }
}

当app前台已经有通知了就不会再发送通知了,因此在创建通知前需要先清空

            UpdateNotificationUtil notificationUtils = UpdateNotificationUtil.getInstance();
            notificationUtils.clearAllNotification();
            notificationUtils.sendNotificationFullScreen("新版本已下载完成", "点击安装", apkFile);
            mContext.startActivity(i);

优化后的installApk方法:

  public static void installApk(Context mContext, File apkFile) {
        if (!apkFile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
            Uri apkFileUri = FileProvider.getUriForFile(mContext,
                    mContext.getPackageName() + ".fileprovider", apkFile);
            i.setDataAndType(apkFileUri, "application/vnd.android.package-archive");
            UpdateNotificationUtil notificationUtils = UpdateNotificationUtil.getInstance();
            notificationUtils.clearAllNotification();
            notificationUtils.sendNotificationFullScreen("新版本已下载完成", "点击安装", apkFile);
            mContext.startActivity(i);
        } else {
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setDataAndType(Uri.parse("file://" + apkFile.toString()),
                    "application/vnd.android.package-archive");
            mContext.startActivity(i);
        }
    }

网上会告诉你这样写已经大功告成了!!!
NO!NO!NO!这样还是会有问题,这样的话,在华为、oppo、vivo部分机型上点击通知栏是不会启动安装程序的,就是点击没有反应。
需要添加全屏通知权限

//AndroidManifest 声明新权限,不用动态申请
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>

这样才是大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青丶穗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值