自定义推送通知声音在奥利奥背景中不起作用

问题描述 投票:1回答:2

我有以下有效负载推送通知,从我的服务器发送。该通知适用于所有版本,但声音仅在Android Oreo上不起作用,其他Android版本工作正常。

{
  "to" : "d4DLcrilLbs...",
   "notification" : {
   "body" : "This is an FCM notification message!",
   "title" : "FCM Message",
   "sound" : "new_sound.wav"
  }
}
android firebase firebase-cloud-messaging android-8.0-oreo
2个回答
0
投票

在oreo中你必须用通道设置声音

请用此更改您的代码

 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
    {
        final Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + this.getPackageName() + "/raw/notification");
        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();
        NotificationChannel channel = new NotificationChannel("MyNotification","MyNotification", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setSound(alarmSound,attributes);
        NotificationManager mgr =getSystemService(NotificationManager.class);
        mgr.createNotificationChannel(channel);

    }

它将适用于您的应用程序在后台/前台


0
投票

Android oreo中的通知需要在渠道下注册,否则您将无法生成通知。

看看以下文章可能对您有所帮助

Click here

© www.soinside.com 2019 - 2024. All rights reserved.