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

问题描述 投票:0回答:5

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

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

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

  1. 我的授权码是正确的
  2. 我正在向
    https://fcm.googleapis.com/fcm/send
  3. 发送 http 请求
  4. 我已经在iPhone 4,iPhone 6和iPhone 6S上测试过,均收到 没有声音的通知
ios firebase firebase-cloud-messaging firebase-notifications
5个回答
71
投票

您的 JSON

"sound" : "default"
应位于
"notification"
键内,而不是位于 JSON 的根目录中。这个 JSON 应该可以工作。

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

38
投票

使用FCM admin 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',
};

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


10
投票
    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


0
投票

我也有同样的问题。当通知通过 FCM 发送到 iOS 时,无法发出声音或振动。我在这里点击了这个链接:https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1 并最终成功了。我在我的有效负载中创建了我的苹果通知作为警报,它对我有用。下面是我的 JSON,以了解我的解决方案。

"apns": {
    "payload": {
        "aps" : {
            "alert" : {
                "body": "Notification body",
                "title": "Notification title"
            },
            "badge" : 2,
            "sound" : "default"
        }
    }
}

注意:请将此“apns”键放在您的消息请求的相关位置。我正在使用 REST 调用来请求消息。 请参阅以下链接,了解如何使用特定于平台的传递选项发送通知消息。 链接:https://firebase.google.com/docs/cloud-messaging/concept-options#example-notification-message-with-platform-specific-delivery-options


0
投票

使用

firebase-admin
节点 js 包

    const message = {
        notification: {
          title: "This is title",
          body: "This is body",
        },
        apns: {
          headers: {
            "apns-priority": "10",
          },
          payload: {
            aps: {
              sound: "default",
            },
          },
        },
        android: {
          notification: {
            sound: "default",
          },
        },
        token: 'fcm-token'
    }


getMessaging()
    .send(message)
    .then((response) => {
      res.json({
        msg: "Sent successfully",
      });
      console.log(response);
    })
    .catch((err) => console.log(err));
© www.soinside.com 2019 - 2024. All rights reserved.