在我的针对23+接口的混合Cordova Android应用程序中,我想使用自定义的声音来发送通知。为此,我做了以下工作
plugin.xml
文件中,用于我在我声明为<resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'
的应用程序中使用的单个自定义插件。将APK作为压缩文件打开,我看到mp3文件实际上以‘`res/raw/mysound.mp3’结尾。-在构建通知时,我执行以下操作
Notification notification = new Notification.Builder(context)
.setDefaults(0) //turns off ALL defaults
.setVibrate(vibrate) /sets to vibrate
....
.setSound(uri).build();
哪里
Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3");
这似乎是我在谷歌上找到的一些文章中指出的秘诀,甚至在其他帖子中也是如此。然而,当我发出通知时,我听不到预期的声音。我可能做错了什么?
下面的答案没有帮助,因为在我的带有自定义插件的混合Cordova应用程序的上下文中,尝试构建APK时会抛出类似于class R not known/found...
的错误
发布于 2018-03-06 14:27:14
下面的代码将帮助您:
String CHANNEL_ID="1234";
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.mysound);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
//For API 26+ you need to put some additional code like below:
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(CHANNEL_ID, Utils.CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(soundUri, audioAttributes);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel( mChannel );
}
}
//General code:
NotificationCompat.Builder status = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
status.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.logo)
//.setOnlyAlertOnce(true)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setVibrate(new long[]{0, 500, 1000})
.setDefaults(Notification.DEFAULT_LIGHTS )
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +mContext.getPackageName()+"/"+R.raw.apple_ring))
.setContentIntent(pendingIntent)
.setContent(views);
mNotificationManager.notify(major_id, status.build());
其中mysound是我的铃声,放在res/raw文件夹下。
备注:您只需输入铃声名称,不带扩展名,如raw/mysound
备注:在Android Oreo中,你必须更改频道ID才能使更改生效。在奥利奥版本之前,使用".setDefaults()“seens阻止自定义声音播放。
发布于 2018-09-24 15:40:22
这些设置是在您第一次创建频道时设置的,然后不会修改,除非您通过刷新安装或清除数据来手动执行此操作。
有关这方面的更多信息,请阅读此处的顶部答案:Android Oreo notification keep making Sound even if I do not set sound. On Older version, works perfectly
发布于 2018-09-26 04:37:43
对于26+接口,需要设置通知频道的声音:
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.siren);
NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(Utils.CHANNEL_SIREN_ID, Utils.CHANNEL_SIREN_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setLightColor(Color.GRAY);
mChannel.enableLights(true);
mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setSound(soundUri, audioAttributes);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel( mChannel );
}
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, Utils.CHANNEL_SIREN_ID)
.setSmallIcon(R.drawable.ic_stat_maps_local_library)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
.setTicker(title)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setAutoCancel(true)
.setLights(0xff0000ff, 300, 1000) // blue color
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
mBuilder.setSound(soundUri);
}
int NOTIFICATION_ID = 1; // Causes to update the same notification over and over again.
if (mNotificationManager != null) {
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
https://stackoverflow.com/questions/48986856
复制相似问题