【问题标题】:FCM Notification in iOS doesn't play sound when receivediOS 中的 FCM 通知在收到时不播放声音
【发布时间】:2017-01-13 02:32:39
【问题描述】:

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

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

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

  1. 我的授权码是正确的
  2. 我正在向https://fcm.googleapis.com/fcm/send发送http请求
  3. 我已经在 iPhone 4、iPhone 6 和 iPhone 6S 上测试过,全部收到 无声通知

【问题讨论】:

  • 我收到来自 fcm 的通知。现在,我想做一些功能,比如我什么时候会从 fcm 收到通知,应用程序应该播放声音文件,甚至不需要触摸通知消息。你知道怎么做吗 ? didRecieveRemoteNotification 只会在用户点击通知时执行。当我收到通知时,我想播放一个声音文件,比如闹钟曲调。请指导我完成它。

标签: ios firebase firebase-cloud-messaging firebase-notifications


【解决方案1】:

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

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

【讨论】:

  • 我在服务器上具有相同的结构,但在某些设备上,当应用程序在后台时,声音文件仍然无法播放。
  • 我收到来自 fcm 的通知。现在,我想做一些功能,比如我什么时候会从 fcm 收到通知,应用程序应该播放声音文件,甚至不需要触摸通知消息。你知道怎么做吗 ? didRecieveRemoteNotification 只会在用户点击通知时执行。当我收到通知时,我想播放声音文件,例如闹钟曲调。请指导我完成它。
  • 这个json文件应该存在哪里?因为我正在通过 ui firebase.com 测试通知,所以我如何发送这个带有声音的有效载荷?
  • 谢谢,它对我有用。请您提供您的答案的来源?谢谢。
  • @DuncanJones,实际上,您的答案在 iOS 12.1 上对我不起作用,但将 sound:'default' 放在非设备特定的 notification 对象中最终对我有用。
【解决方案2】:

使用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 设置)

【讨论】:

  • 好电话 - 我找不到任何关于让通知声音在任何地方都能在 IOS 中工作的信息,我没有意识到它是 APNS 对象的一部分!为此干杯。
【解决方案3】:
    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

【讨论】:

    【解决方案4】:

    我遇到了同样的问题。当通知通过 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

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      相关资源
      最近更新 更多