如何制作带有特定声音的推送通知?
How to make push notification with specific sound?
我正在开发带有通知系统的 android 应用程序,我需要 android 设备以我存储在资产文件夹中的特定声音推送通知
这是我的 java 通知代码:
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("Calcupital")
.setContentTitle("Calcupital")
.setContentText("User Information has been updated successfully")
.setSmallIcon(R.drawable.login)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
假设我的声音是这样存储的:(\assets\hopo.mp3)
如何通过更改 android 设备提供的列表中的声音,在不更改其他应用程序的推送通知系统的情况下使用此声音推送此通知!!
我希望你很清楚我的问题:)
您可以使用
设置默认声音
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmSound != null) {
mBuilder.setSound(alarmSound);
}
如果您想要自定义声音,只需使用不同的 Uri
。如何从资产中获取 Uri
? check this topic
您需要将声音文件放到 res/raw 目录。
然后将这段代码添加到当前代码中
final String packageName = context.getPackageName();
notification.sound =
Uri.parse("android.resource://" + packageName + "R.raw.hopo");
查看此 link 了解更多详情
http://developer.android.com/reference/android/app/Notification.Builder.html#setSound(android.net.Uri)
合并此处的答案并使用这些问题的两个答案:
- How to add sound to notification?
- How to get URI from an asset File?
试试这个:
Uri sound = Uri.parse("file:///android_asset/hopo.mp3");
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("Calcupital")
.setContentTitle("Calcupital")
.setContentText("User Information has been updated successfully")
.setSmallIcon(R.drawable.login)
.setSound(sound);
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
获取资源:
Uri alarmSound = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.somefile);
** 必须在 res 文件夹中创建原始文件夹并添加你的声音文件 **
要设置:
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setContentText(message)
.setSound(alarmSound) // -> add this one
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
我正在开发带有通知系统的 android 应用程序,我需要 android 设备以我存储在资产文件夹中的特定声音推送通知 这是我的 java 通知代码:
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("Calcupital")
.setContentTitle("Calcupital")
.setContentText("User Information has been updated successfully")
.setSmallIcon(R.drawable.login)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
假设我的声音是这样存储的:(\assets\hopo.mp3)
如何通过更改 android 设备提供的列表中的声音,在不更改其他应用程序的推送通知系统的情况下使用此声音推送此通知!!
我希望你很清楚我的问题:)
您可以使用
设置默认声音Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmSound != null) {
mBuilder.setSound(alarmSound);
}
如果您想要自定义声音,只需使用不同的 Uri
。如何从资产中获取 Uri
? check this topic
您需要将声音文件放到 res/raw 目录。
然后将这段代码添加到当前代码中
final String packageName = context.getPackageName();
notification.sound =
Uri.parse("android.resource://" + packageName + "R.raw.hopo");
查看此 link 了解更多详情 http://developer.android.com/reference/android/app/Notification.Builder.html#setSound(android.net.Uri)
合并此处的答案并使用这些问题的两个答案:
- How to add sound to notification?
- How to get URI from an asset File?
试试这个:
Uri sound = Uri.parse("file:///android_asset/hopo.mp3");
Notification noti = new Notification.Builder(MainActivity.this)
.setTicker("Calcupital")
.setContentTitle("Calcupital")
.setContentText("User Information has been updated successfully")
.setSmallIcon(R.drawable.login)
.setSound(sound);
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
获取资源:
Uri alarmSound = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.somefile);
** 必须在 res 文件夹中创建原始文件夹并添加你的声音文件 **
要设置:
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setContentText(message)
.setSound(alarmSound) // -> add this one
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });