【问题标题】:No custom sound with Android Firebase NotificationAndroid Firebase 通知没有自定义声音
【发布时间】:2019-03-26 12:23:32
【问题描述】:

我在我的 Android 应用中使用 Firebase 推送通知。我可以使用自定义图标正确发送通知,但我没有设法播放我的自定义声音。我总是得到设备的默认声音。

{
    "registration_ids": "myToken",
    "notification": {
        "body": "my body",
        "title": "my title",
        "icon": "ic_notification",
        "sound": "mysound.mp3" // I tried "mysound", "mysound.wav"...

    },
    "priority": "high"

}

自定义声音位于/res/raw

我已经能够使用onMessageReceived 和 Firebase 数据消息播放我的自定义声音但不能使用 Firebase 通知消息

我的安卓设备是小米A1和奥利奥8.1,也试过小米A2,结果一样。

我尝试使用 php 和 curl,使用 node.js...总是同样的问题,我得到了我的默认声音。

更新

使用 node.js 的此代码也不起作用:

var registrationToken = 'xxxxxx';

var message = {

    notification: {
      title: 'my title',
      body: 'my body',
    },
    android: {
      ttl: 3600 * 1000,
      notification: {
        color: '#ff0000',
        sound: 'mysound.mp3'
      }
    }, 
    token: registrationToken

};

【问题讨论】:

标签: android firebase firebase-cloud-messaging android-8.1-oreo


【解决方案1】:

我终于找到了解决方案。对于 Android 8.0 及更高版本,需要在您的应用中创建通知通道:

NotificationChannel channel = new NotificationChannel('my_id', name, importance);

(更多信息:https://developer.android.com/training/notify-user/channels#java

那么当你发送通知时:

var registrationToken = 'xxxxxx';

var message = {

    notification: {
      title: 'my title',
      body: 'my body',
    },
    android: {
      ttl: 3600 * 1000,
      notification: {
        color: '#ff0000',
        sound: 'mysound.mp3',
        channel_id: 'my_id' // important to get custom sound
      }
    }, 
    token: registrationToken

};

【讨论】:

【解决方案2】:

我也在寻找android中firebase通知自定义声音的解决方案,我已经通过Notification Channel解决了这个问题。

我创建了一个带有自定义声音的通知通道,该声音在应用程序后台状态下收到通知后播放。

您可以参考以下通知渠道的链接。

https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c

https://developer.android.com/training/notify-user/channels

您需要将您的 mp3 文件放在 /res/raw/ 路径。

请找到代码。

NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NotificationManager.class); // If you are writting code in fragment

NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); // If you are writting code in Activity

createNotificationChannel 函数

private void createNotificationChannel() { 
 Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.sample); //Here is FILE_NAME is the name of file that you want to play 
// Create the NotificationChannel, but only on API 26+ because 
// the NotificationChannel class is new and not in the support library if 
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
 { 
    CharSequence name = "mychannel"; 
    String description = "testing"; 
    int importance = NotificationManager.IMPORTANCE_DEFAULT; 
    AudioAttributes audioAttributes = new AudioAttributes.Builder() 
     .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
     .setUsage(AudioAttributes.USAGE_ALARM) 
     .build(); 
   NotificationChannel channel = new NotificationChannel("cnid", name, importance); 
   channel.setDescription(description); 
   channel.enableLights(true); channel.enableVibration(true); 
   channel.setSound(sound, audioAttributes); 
   notificationManager.createNotificationChannel(channel); 
  } 
};

createNotificationChannel(); 

要实现这一点,您需要在 firebase 通知请求对象中传递 android_channel_id 属性。

{
 "notification": {
 "body": "this is testing notification",
 "title": "My App",
 "android_channel_id": "cnid"
 },
 "to": "token"
}

注意 - 如果您创建了一次通知频道,则无法更改声音。您必须使用新名称和您想要的声音创建一个新的通知频道。

【讨论】:

    【解决方案3】:

    documentation 开始,将声音包含在android 对象下的通知对象中。在声音值中给出声音文件的名称。声音文件必须位于 /res/raw/ 中。下面是一个 Node.js 示例:-

      var message = {
      notification: {
        title: 'sample title',
        body: 'Hello, its Tuesday.',
      },
      android: {
        ttl: 3600 * 1000,
        notification: {
          icon: 'my_icon',
          color: '#f45342',
          sound: 'filename.mp3',
        },
      },
      apns: {
        payload: {
          aps: {
            badge: 42,
          },
        },
      },
      topic: 'industry-tech'
    };
    

    【讨论】:

    • 当应用程序在后台使用 notification 负载时,您是否收到通知?
    • 是的,当应用程序处于后台时我会收到通知,但我会收到默认声音
    【解决方案4】:

    尝试从 Firebase 控制台启用声音

    更多信息请看这个答案

    https://stackoverflow.com/a/44304089/9024123

    可能这应该是问题,并尝试删除声音元素中的'.mp3'并使其像

    "sound":"mySound"
    

    【讨论】:

      【解决方案5】:

      我也面临同样的问题。我总是得到默认声音,但我将其修复如下。我正在使用 FCM-push(节点模块)https://www.npmjs.com/package/fcm-push

      var message = {  
      to : device_token,
      collapse_key : '<insert-collapse-key>',
      // data : {
      //     '<random-data-key1>' : '<random-data-value1>',
      //     '<random-data-key2>' : '<random-data-value2>'
      // },
      notification : {
          title : 'Title ',
          body : 'some Body',
          sound : 'notification' // my .ogg file in /res/raw
      },
      android: {
      sound: 'notification' // my .ogg file name in /res/raw
      }
      };
      

      我没有用 mp3 或 wav 尝试过,在您的问题中,您似乎没有尝试过 .ogg 文件(尽管我怀疑它是否与音频格式有关,但您可以尝试)

      【讨论】:

        【解决方案6】:

        也许这会有所帮助: 在我的情况下,我尝试使用上述方法但它没有工作,每当我检查 onMessageReceived (出于调试目的)通道 id

        Log.e(TAG, "Message Notification channel id: " + remoteMessage.getNotification().getChannelId()); 
        

        我总是得到'null'。

        所以从这里阅读文档https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

        我发现我在 json 中使用了错误的频道 ID 键。

        尝试使用 'android_channel_id' 键而不是 'channel_id' 键

        "notification": {
                "body": "Body of Your Notification",
                "title": "Title of Your Notification",
                "android_channel_id": "channelId",
                "icon": "myIcon"
            }
        

        如果我按预期使用它,则会播放自定义声音(来自 res/raw)

        PS:就我而言,我在创建它时将声音设置在 channelId 上

        祝你好运!

        【讨论】:

          【解决方案7】:

          我在尝试此操作时犯的一个错误是我没有更改频道 ID,尽管我更新了 Json 并修改了代码。我的频道总是绑定到设备声音。就在我更新到不同的频道 ID 之后,我的代码开始工作。现在正在为前景和背景播放自定义声音

          以下是对我有用的 JSON

          {
          "to" : "token"
          ,
          "notification": {
              "title":"Server Notification Title",
              "body":"Notification Body",
              "sound":"sound.wav",
              "android_channel_id": "ch1"
          },
          "priority": "high"
          }
          

          代码

          Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.sound);
              NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "ch1")
                      .setSmallIcon(R.mipmap.ic_launcher)
                      .setContentTitle(getString(R.string.app_name))
                      .setContentText(remoteMessage.getNotification().getBody().toString())
                      .setAutoCancel(true)
                      .setSound(soundUri);
          
              NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                  if(soundUri != null){
                      // Changing Default mode of notification
                      notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                      // Creating an Audio Attribute
                      AudioAttributes audioAttributes = new AudioAttributes.Builder()
                              .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                              .setUsage(AudioAttributes.USAGE_ALARM)
                              .build();
          
                      // Creating Channel
                      NotificationChannel channel = new NotificationChannel("ch1","App Name",NotificationManager.IMPORTANCE_HIGH);
                      channel.setSound(soundUri,audioAttributes);
                      channel.enableLights(true); channel.enableVibration(true);
                      mNotificationManager.createNotificationChannel(channel);
                  }
              }
              mNotificationManager.notify(0, notificationBuilder.build());
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-23
            • 1970-01-01
            相关资源
            最近更新 更多