android 8.0及以上推送收不到问题解决

2019-06-19  本文已影响0人  北漂攻城狮的泪

1、部分手机无法启动通知权限手动启动

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

settingPermission();//设置通知权限8.0/9.0

}

/**

* 设置通知权限8.0/9.0

*/

private void settingPermission() {

if (!checkNotifySetting()){

try {

// 根据isOpened结果,判断是否需要提醒用户跳转AppInfo页面,去打开App通知权限

            Intent intent =new Intent();

intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);

//这种方案适用于 API 26, 即8.0(含8.0)以上可以用

            intent.putExtra(EXTRA_APP_PACKAGE, getPackageName());

intent.putExtra(EXTRA_CHANNEL_ID, getApplicationInfo().uid);

//这种方案适用于 API21——25,即 5.0——7.1 之间的版本可以使用

            intent.putExtra("app_package", getPackageName());

intent.putExtra("app_uid", getApplicationInfo().uid);

// 小米6 -MIUI9.6-8.0.0系统,是个特例,通知设置界面只能控制"允许使用通知圆点"——然而这个玩意并没有卵用,我想对雷布斯说:I'm not ok!!!

//  if ("MI 6".equals(Build.MODEL)) {

//      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

//      Uri uri = Uri.fromParts("package", getPackageName(), null);

//      intent.setData(uri);

//      // intent.setAction("com.android.settings/.SubSettings");

//  }

            startActivity(intent);

}catch (Exception e) {

e.printStackTrace();

// 出现异常则跳转到应用设置界面:锤子坚果3——OC105 API25

            Intent intent =new Intent();

//下面这种方案是直接跳转到当前应用的设置界面。

//https://blog.csdn.net/ysy950803/article/details/71910806

            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

Uri uri = Uri.fromParts("package", getPackageName(),null);

intent.setData(uri);

startActivity(intent);

}

}

}

private boolean checkNotifySetting() {

NotificationManagerCompat manager = NotificationManagerCompat.from(this);

// areNotificationsEnabled方法的有效性官方只最低支持到API 19,低于19的仍可调用此方法不过只会返回true,即默认为用户已经开启了通知。

        boolean isOpened = manager.areNotificationsEnabled();

//        if (isOpened) {

//          ToastUtils.showShort(this,"通知权限开启成功");

//

//        } else {

////            ToastUtils.showShort(this,"还没有开启通知权限,点击去开启");

//        }

        return isOpened;

}

2、动态配置广播(在build.gradle中 adnroid-buildTypes下配置信息 ):

1、动态配置

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

intentFilter =new IntentFilter()

            intentFilter.addAction(getString(R.string.getui_action,BuildConfig.GETUI_APP_ID));

receiver =new GexinMsgReceiver();

registerReceiver(receiver,intentFilter);

}

2、action信息动态配置

buildTypes {

debug {

buildConfigField"boolean","LOG_DEBUG","true"

        proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.txt'

        versionType ="debug"

        signingConfig signingConfigs.signingConfig

manifestPlaceholders = [

GETUI_APP_ID    :"xxxxxxxxxxx",

GETUI_APP_KEY  :"xxxxxxxxxxx",

GETUI_APP_SECRET:"xxxxxxxxxxx"

        ]

buildConfigField"String","GETUI_APP_ID","\"${manifestPlaceholders.GETUI_APP_ID}\""//个推ID

    }

release {

buildConfigField"boolean","LOG_DEBUG","false"

        minifyEnabledtrue

        shrinkResourcestrue

        zipAlignEnabledtrue

        signingConfig signingConfigs.signingConfig

proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.txt'

        versionType ="release"

        manifestPlaceholders = [

GETUI_APP_ID    :"xxxxxxxxxxx",

GETUI_APP_KEY  :"xxxxxxxxxxx",

GETUI_APP_SECRET:"xxxxxxxxxxx"

        ]

buildConfigField"String","GETUI_APP_ID","\"${manifestPlaceholders.GETUI_APP_ID}\""//个推ID

    }

}

上一篇 下一篇

猜你喜欢

热点阅读