Android通知setSound不工作
在我的以API 23+为目标的Cordova Android混合应用程序中,我想使用自定义声音进行通知。为此,我做了以下工作Android通知setSound不工作,android,uri,android-notifications,android-6.0-marshmallow,Android,Uri,Android Notifications,Android 6.0 Marshmallow,在我的以API 23+为目标的Cordova Android混合应用程序中,我想使用自定义声音进行通知。为此,我做了以下工作 Notification notification = new Notification.Builder(context) .setDefaults(0) //turns off ALL defaults .setVibrate(vibrate) /sets to vibrate .... .setSound(uri).build
Notification notification = new Notification.Builder(context)
.setDefaults(0) //turns off ALL defaults
.setVibrate(vibrate) /sets to vibrate
....
.setSound(uri).build();
- 在
文件中,为我在应用程序中使用的单个自定义插件声明plugin.xml
李>'
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时抛出了一个错误,即
类R未知/未找到…
您正在访问参考资料子文件夹中的声音
将uri的源更改为
Uri uri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.siren);
对于默认声音,请使用:
notification.defaults |= Notification.DEFAULT_SOUND;
您可以在处理通知时调用此方法
public void playNotificationSound() {
try {
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
Ringtone r = RingtoneManager.getRingtone(mContext, alarmSound);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
我不确定,但我认为问题在于您的做法是错误的
“/raw/mysound.mp3
:
Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3");
首先在清单中添加权限:使用权限android:name=“android.permission.VIBRATE”/>
然后您可以设置默认声音,如下所示:-
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
对于振动:
mBuilder.setVibrate(new long[] { 1000, 1000});
对于自定义声音,请将mp3文件放在以下路径:Res\raw\sound.mp3
然后
Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.sound);
用这个来设置声音
Uri defaultSoundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + mContext.getPackageName() + "/raw/mysound");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(mContext)
.setContentIntent(mainPIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setContentTitle("" + title)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentText("" + body);
NotificationManager mNotificationManager =
(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(title, NOTIFICATION_ID, mBuilder.build());
下面的代码将帮助您:
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这样的分机名
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(mTitle);
builder.setContentText(mContentText);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
builder.setDefaults(Notification.DEFAULT_ALL);
用它来处理声音,我希望它能解决你的问题,干杯
对于API 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());
}
urisounduri=Uri.parse(ContentResolver.SCHEME\u ANDROID\u RESOURCE+“:/”+getApplicationContext().getPackageName()+“/”+R.raw.siren);
NotificationManager mNotificationManager=(NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION\u服务);
通知通道;
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.O){
mcchannel=新通知通道(Utils.CHANNEL\u SIREN\u ID,Utils.CHANNEL\u SIREN\u NAME,NotificationManager.IMPORTANCE\u HIGH);
mcchannel.setLightColor(Color.GRAY);
mChannel.enableLights(真);
mcchannel.setDescription(Utils.CHANNEL\u SIREN\u DESCRIPTION);
AudioAttributes AudioAttributes=新建AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT\u TYPE\u声音化)
.setUsage(AudioAttributes.USAGE_通知)
.build();
mcchannel.setSound(soundUri、audioAttributes);
if(mNotificationManager!=null){
mNotificationManager.createNotificationChannel(mChannel);
}
}
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(此,Utils.CHANNEL\u SIREN\u ID)
.setSmallIcon(R.drawable.ic\u stat\u maps\u local\u库)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),R.mipmap.ic_启动器))
.setTicker(标题)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setAutoCancel(真)
.setLights(0xff0000ff,3001000)//蓝色
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_默认值);
if(Build.VERSION.SDK\u INT
唯一的问题是当使用R.raw.mysound.mp3
时,我无法编译应用程序-错误R无法解析为变量
。请记住,我有一个带有自定义插件的Cordova项目。我将通知、setsound等代码与有问题的声音资源一起放入该插件中。我接受y我们的答案是因为它最接近于确定潜在问题-是扩展名mp3
导致了问题。没有扩展名,我的原始代码提供了所需的结果。请注意,在Cordova应用程序中R.java
未被识别,因此您上面显示的内容并不真正适用。您为什么要调用setDefaults
两次?我在AudioAttributes
中找不到CONTENT\u TYPE\u NOTIFICATION
,所以我忽略了这一点,解决方案仍然有效。在Android Oreo中,您必须更改频道ID才能使更改生效。在Oreo版本之前,使用“.setDefaults()“seens阻止自定义声音播放。@Thiagoy这是必须的。我花了一天的时间来纠正这个问题。在这里投票是因为我怀疑使用ContentResolver
而不是android。参考资料://
是一种更加经得起未来考验的做事方式。您在这里通过RingToneManager所做的工作是有效的,但这是不对的,并且在收到通知后不应该这样做。Builder已经它自己的设置声音的方法是安卓系统中有很多不应该做的事情,我在过去的4年里学到了,不要认为安卓系统中的任何事情都能正常工作或者已经为你做了,直到你亲眼看到它…仅供参考mcchannel.setSound(soundUri,AudioAttrib