Android 自定义通知声音
我的andriod应用程序中有Firebase消息。我正在使用Firebase发送推送通知。我想将默认通知声音更改为自定义声音。我怎么做Android 自定义通知声音,android,firebase,notifications,firebase-cloud-messaging,Android,Firebase,Notifications,Firebase Cloud Messaging,我的andriod应用程序中有Firebase消息。我正在使用Firebase发送推送通知。我想将默认通知声音更改为自定义声音。我怎么做 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
Uri defaultSoundUri =
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_notif)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setCustomBigContentView(remoteViews)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(id) /* ID of notification */, notificationBuilder.build());
}
使用上述代码从参考资料中添加自定义声音
如果我们使用通知类,则可以使用上面的代码
Notification notification = new Notification(icon, tickerText, when);
在使用NotificationBuilder时,请使用以下代码
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_notif)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pendingIntent)
.setCustomBigContentView(remoteViews)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
;
使用setSound()
方法设置声音
if(!silent) { // check if phone is not in silent mode
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(9999, notificationBuilder.build());
}
}
或者你可以使用
{
"to" : "XXYYXXYY...",
"notification" : {
"body" : "The stock opened on a bullish note at Rs. 449 and touched a high of Rs. 461.35, up 5.06 per cent over its previous closing price on the BSE. A similar movement was seen on the NSE where the stock opened at Rs. 450 and hit a high of Rs. 463.70, up 5.32 per cent.",
"title" : "Stocks in focus: Kalpataru Power, Punj Lloyd, J B Chem, Bharti Airtel",
"icon" : "ic_stock",
"sound" : "res_notif_sound"
}
}
如果要使用设备的默认声音,应使用:
“声音”:“默认”
你们会发现这篇文章很有用。它解释了如何处理自定义声音。我应该在哪里替换它?我们可以用我们的代码修改上面的代码吗?它[有效]非常感谢兄弟:)它帮了我很多:)我很乐意帮忙。请接受正在运行的答案。:)@JackN,无法理解您的评论如果声音是系统文件?如果我们想引用res文件夹中的文件。怎么做?谢谢,它帮了我很多。@AnuChaudhary很乐意帮忙;)
{
"to" : "XXYYXXYY...",
"notification" : {
"body" : "The stock opened on a bullish note at Rs. 449 and touched a high of Rs. 461.35, up 5.06 per cent over its previous closing price on the BSE. A similar movement was seen on the NSE where the stock opened at Rs. 450 and hit a high of Rs. 463.70, up 5.32 per cent.",
"title" : "Stocks in focus: Kalpataru Power, Punj Lloyd, J B Chem, Bharti Airtel",
"icon" : "ic_stock",
"sound" : "res_notif_sound"
}
}