關於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"/>

這樣纔是大功告成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章