在 iOS 12 从 FCM 收到远程推送通知时播放自定义声音

Playing a custom sound on receiving a remote push notification on iOS 12 from FCM

我正在尝试在应用完全关闭或在后台收到 REMOTE 通知时播放自定义声音,但我尝试过的所有操作都无效没用。

我使用的音频文件是支持的.caf 文件,长度为20 秒,在苹果的限制范围内。该文件位于主应用程序包中,而不是在特定方案中。我的远程通知注册是在 .Sound 注册的。

这是我的 XCode 项目经理的屏幕截图,显示了我的自定义声音所在的位置。

从 firebase 函数发送远程通知的代码:

userData.tokens.forEach(async token => {
    console.log("Token: "+token);
    const message = {
        data: {
            type: "type"
        },
        android: {
            priority: "high"
        },                  
        notification: {
            "title": "Title of message",
            "sound": "customSound.caf"
        },
        apns: {
            headers: {
                "apns-priority": "10"
            },
            payload: {
                aps: {
                    sound: "customSound.caf"
                },
                    sound: "customSound.caf"
                }
            },
            token: token
        };
    console.log(message);
    await admin.messaging().send(message);
});

这是我的 AppDelegate 中的注册码:

if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
    UNUserNotificationCenter.current().delegate = self
    Messaging.messaging().delegate = self
} else {
    let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

我什至尝试过重启我的设备,因为这是我在另一个 Stack Overflow 上读到的错误,但没有任何作用。如果有帮助,我是 运行 iOS 12.1.4 第 6 代 iPad。

感谢任何建议。

编辑 只是忘了,我的 info.plist 文件中也有这个

<key>UILocalNotificationDefaultSoundName</key>
    <string>OnceAgainNotification.caf</string>  

通过 firebase 发送的声音名称必须与声音文件的名称及其扩展名相匹配。

此外,请确保文件在构建阶段已在项目中正确实施。

关于默认声音,通过 info.plist 更改默认声音名称在 iOS 10.

之后被弃用

https://developer.apple.com/documentation/uikit/uilocalnotificationdefaultsoundname