About the problem that clicking the notification on Android Q cannot jump

On Android Q, that is, api 29, there may be a problem that clicking on the notification using PendingIntent cannot jump, but you can’t find the problem after searching for a long time. For example, you will encounter this problem when the app is automatically updated to be compatible with android Q. After downloading and installing, there is no problem when the app is in the foreground, but there will be problems when it is in the background, because Google prohibits background startup activities on Android Q, so when you set in gradle:

targetSdkVersion 29

Then it will appear that the system installer cannot be started when the app is in the background, so after consulting the information, someone will tell you to write like this:
app is compatible with Android Q to start the installer Compatible code:

    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);
        }
    }

First of all, you will find that there is no problem in writing the foreground installation like this, but there will be problems in the background installation, and then use the method of clicking the notification to start the installer to be compatible with android Q to start the activity in the background:

/**
 * 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();
        }
    }
}

When there is already a notification in the foreground of the app, no more notifications will be sent, so it needs to be cleared before creating a notification

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

Optimized installApk method:

  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);
        }
    }

The Internet will tell you that writing like this is done! ! !
NO! NO! NO! In this case, there will still be problems. In this case, clicking the notification bar on some models of Huawei, oppo, and vivo will not start the installation program, but the click will not respond.
Need to add full screen notification permission

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

That's it!

Guess you like

Origin blog.csdn.net/fzkf9225/article/details/107151777