奥利奥仅在 FCM 中播放默认通知



当通知来临时,棉花糖和牛轧糖一切都很好,但是当我在奥利奥通知中测试我的应用程序时,但它播放了手机中选择的defalut声音。 我正在测试 2-3 奥利奥手机,但结果相同。 这是我的通知代码

private void showNotificationMessage(Context context, String title, String message, Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "channel-01";
String channelName = "Channel Name";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.mipmap.vb_grey)
.setColor(Color.RED)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic))
// .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

}

谁能告诉我我哪里做错了。

在奥利奥中,通知声音设置Notification Builder将不起作用。您需要创建一个NotificationChannel,并需要在NotificationManager上设置它以使其按如下方式工作

Uri sound = Uri.parse("android.resource://"+context.getPackageName()+"/"+R.raw.offic);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR CHANNEL NAME",
NotificationManager.IMPORTANCE_DEFAULT)
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);

mChannel.setDescription(msg);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // Here you set the sound

if (mNotificationManager != null)
mNotificationManager.createNotificationChannel(mChannel);
}

如果已使用通知通道,则首次无法正常工作。您需要创建其他通道并为其设置声音才能使其正常工作。您可以使用以下代码检查和删除创建的多个通道。

if (mNotificationManager != null) {
List<NotificationChannel> channelList = mNotificationManager.getNotificationChannels();
for (int i = 0; channelList != null && i < channelList.size(); i++) {
mNotificationManager.deleteNotificationChannel(channelList.get(i).getId());
}
}

最新更新