iOS 中的 FCM 通知在收到时不播放声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39335363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:03:18  来源:igfitidea点击:

FCM Notification in iOS doesn't play sound when received

iosfirebasefirebase-cloud-messagingfirebase-notifications

提问by Jr Pro

I am using Firebase push notifications in my iOS Application. Although I am able to send the notification by sending below payload, it doesn't play a sound when received.

我在我的 iOS 应用程序中使用 Firebase 推送通知。虽然我可以通过发送以下有效负载来发送通知,但它在收到时不会播放声音。

{
    "to": "myToken",
    "notification": {
        "body": "test",
        "title": "test"
    },
    "priority": "high"
    "sound": "default"
}

If I send the test message from console, it works well and plays notification sound.
Note:

如果我从控制台发送测试消息,它运行良好并播放通知声音。
注意

  1. My Authorization code is correct
  2. I am sending http request to https://fcm.googleapis.com/fcm/send
  3. I have tested it on IPhone 4 , IPhone 6 and IPhone 6S, All recieve notifications without sound
  1. 我的授权码是正确的
  2. 我正在发送 http 请求到 https://fcm.googleapis.com/fcm/send
  3. 我已经在 iPhone 4、IPhone 6 和 iPhone 6S 上测试过,所有收到通知都没有声音

回答by kaitosenpai

your JSON "sound" : "default"should be inside the "notification"key not in the root of the JSON. This JSON should work.

您的 JSON"sound" : "default"应该在"notification"密钥内,而不是在 JSON 的根目录中。这个 JSON 应该可以工作。

{
    "to": "myToken",
    "notification": {
         "body": "test",
         "title": "test",
         "sound": "default"
    },
    "priority": "high"
}

回答by Duncan Jones

When using the FCM admin SDK, you have to specify sounds separately for Android and Apple devices:

使用FCM 管理 SDK 时,您必须为 Android 和 Apple 设备分别指定声音:

let message = {
    notification: {
        'body': 'This is the message the user sees',
    },
    data: {
        'param1': 'specify some extra data here',
    },
    // Apple specific settings
    apns: {
        headers: {
            'apns-priority': '10',
        },
        payload: {
            aps: {
                sound: 'default',
            }
        },
    },
    android: {
      priority: 'high',
      notification: {
          sound: 'default',
      }
    },
    token: 'target FCM token goes here',
};

(Note: I've only tested the Apple settings thus far)

(注意:到目前为止,我只测试了 Apple 设置)

回答by Yaroslav Malyk

    payload = {
        notification:{
            title: 'SOLO has been changed by an administrator',
            body: 'Administrator changed your SOLO schedule',
        },
        android: {
        },
        apns: {
            headers:{
                "apns-collapse-id": "solo_changed_administrator",
                "content-available": "1",
                "apns-priority": "10",
            },
            payload:{
                aps:{
                    sound: 'default',
                    badge: 12213123223
                }
            }
        },
        data:{
            type: 'type'
        }
    }

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig