【发布时间】:2019-01-08 06:28:09
【问题描述】:
我试图通过在原始文件夹中添加 .wav 文件来产生两种类型的自定义通知声音,当通知之类的工作出现时,它会产生与工作相关的通知声音,否则如果出现消息之类的通知,它会产生消息相关的通知声音。
如果要在高端设备中获取通知,我设置了频道,但是在创建频道 ID 后,当根据哪种类型的通知先出现通知时,我会得到相同的通知声音。
onMessageReceived
String sound = data.get("sound");
Uri soundUri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + sound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setSmallIcon(getNotificationIconId())
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && notificationManager != null) {
int importance = android.app.NotificationManager.IMPORTANCE_HIGH;
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(
getString(R.string.default_notification_channel_id), Constants.NOTIFICATION_CHANNEL_NAME_MESSAGE, importance);
mChannel.setSound(soundUri,audioAttributes);
notificationManager.createNotificationChannel(mChannel);
}
if (notificationManager != null) {
notificationManager.notify(messageId, notificationBuilder.build());
}
清单
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>`enter code here`
【问题讨论】:
标签: android push-notification android-notifications