Android 通知 setSound 不工作

新手上路,请多包涵

在我针对 API 23+ 的混合 Cordova Android 应用程序中,我想使用自定义声音进行通知。为此,我做了以下事情

  • plugin.xml 我在应用程序中使用的单个自定义插件的文件中,我声明 <resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'

打开 APK 作为 zip 存档,我看到 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");

这似乎是我在谷歌搜索上什至在 SO 的其他线程中找到的许多文章中指出的方法。然而,当我发出通知时,我没有听到预期的声音。我可能做错了什么?


下面的答案没有帮助,因为在我的带有自定义插件的混合 Cordova 应用程序的上下文中,尝试构建 APK 会抛出错误 class R not known/found...

原文由 DroidOS 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 920
2 个回答

下面的代码将帮助你:

  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 才能使更改生效。在 Oreo 版本之前,使用“.setDefaults()”会阻止播放自定义声音。

原文由 Ketan Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题