当targetSdkVersion大于等于26时, 在8.0及以上机型,Notification都要指定NotificationChannel,不指定NotificationChannel会导致Push不能展示在通知栏,为不影响Photon Push SDK产生的通知栏展示,接入方需要做以下适配:
- Photon Push SDK升级到2.1.0以上版本
- 接入方客户端自行创建和维护NotificationChannel,可根据业务类型创建不同的NotificationChannel,通道对应的描述文案、铃声、震动设置等由接入方根据业务类型自定义
public static final String NOTIFICATION_CHANNEL_ID_DEFAULT = "notification.default";
public static final String NOTIFICATION_CHANNEL_ID_MSG = "notification.msg";
public static final String NOTIFICATION_CHANNEL_ID_OTHERS = "notification.others";
void createDefaultChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationManager nm = (NotificationManager) context.getSystemService(
NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID_DEFAULT);
if (null == notificationChannel) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_DEFAULT, "默认通知", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("消息推送默认通知类别");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{50, 100});
nm.createNotificationChannel(notificationChannel);
}
}
void createMSGChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationManager nm = (NotificationManager) context.getSystemService(
NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID_MSG);
if (null == notificationChannel) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_MSG, "新消息通知", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("收到新消息时使用的通知类别");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{50, 100});
notificationChannel.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.ms2), new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build());
nm.createNotificationChannel(notificationChannel);
}
}
void createOtherChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return;
}
NotificationManager nm = (NotificationManager) context.getSystemService(
NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID_OTHERS);
if (null == notificationChannel) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_OTHERS, "其他通知", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription("其他不重要消息的通知类别");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.enableVibration(false);
nm.createNotificationChannel(notificationChannel);
}
}
建议在Application onCreate方法中调用;调用以上方法创建NotificationChannel后,在系统设置-应用通知下会产生通知类别列表(某些国产手机通道类型数为1时不显示列表)
- 服务端Api推送需传入channelId;使用推送后台推送需要输入channelId
- 需要注意的是,使用targetSdkVersion大于等于26编译,运行在Android 8.0及以上的应用,服务端传的铃声、震动等设置将不再生效,取而代之的是channelId对应NotificationChannel创建时的设置或用户在系统通知设置更改后的设置;可引导用户跳转到NotificationChannel的设置页面
void gotoDefaultChannelSetting() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, MyApplication.NOTIFICATION_CHANNEL_ID_DEFAULT);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(intent);
}
}
调用以上方法,传入指定channelId,可跳转到对应NotificationChannel的系统设置页