Android通知setSound无法正常工作 [英] Android notification setSound is not working

查看:843
本文介绍了Android通知setSound无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在面向API 23+的混合式Cordova Android应用中,我想使用自定义声音进行通知.为此,我已完成以下操作

In my hybrid Cordova Android app targeting API 23+ I want to use a custom sound for notifications. To that end I have done the following

  • 在我在应用程序中使用的单个自定义插件的plugin.xml文件中,声明了<resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'.
  • In plugin.xml file for the single custom plugin I use in the app I declare <resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'.

打开APK作为zip存档,我发现mp3文件实际上以`res/raw/mysound.mp3结尾. -建立通知时,请执行以下操作

Opening the APK as a zip archive I see that the mp3 file has in fact ended up in `res/raw/mysound.mp3'. - When building the notification I do the following

    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");

这似乎是我在谷歌搜索的地方甚至在SO的其他主题中找到的许多文章中指出的配方.但是,当我发出通知时,我听不到预期的声音.我可能做错了什么?

This appears to be the recipe indicated in a number of articles I find on a spot of googling and even in other threads on SO. And yet, when I issue a notification I do not hear the expected sound. What might I be doing wrong?

下面的答案无济于事,因为在我的带有自定义插件的混合式Cordova应用程序的上下文中,尝试构建APK时会沿着class R not known/found...

The answer below does not help since in the context of my hybrid Cordova app with a custom plugin attempting to build the APK throws up an error along the lines of class R not known/found...

推荐答案

以下代码将帮助您:

 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文件夹下.

where mysound is my ringtone which is put under res/raw folder.

注意:您只需输入铃声名称,无需扩展名即可,例如 raw/mysound

Note: you have to only put name of ringtone without extension like raw/mysound

这篇关于Android通知setSound无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆