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

该博客主要介绍了如何在Android 8.0及以上版本动态设置应用的通知权限,包括检查和启动权限设置的代码实现。同时,还详细讲解了在build.gradle中动态配置广播接收器的过程,特别是针对不同系统版本的适配策略。
摘要由CSDN通过智能技术生成

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值