在从FCM接收到iOS 12上的远程推送通知时播放自定义声音 [英] Playing a custom sound on receiving a remote push notification on iOS 12 from FCM

查看:251
本文介绍了在从FCM接收到iOS 12上的远程推送通知时播放自定义声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序完全关闭或在后台关闭时收到 REMOTE 通知时,我正在尝试播放自定义声音,但是我尝试执行的所有操作均无法正常工作.

I am trying to get a custom sound to play when a REMOTE notification is received while the app is either closed completely or in the background, but everything that I have tried has not worked.

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

The Audio file that I am using is a .caf file which is supported, it is 20 seconds long which is within apple's limits. The file is located in the main app bundle, not within a specific scheme. and my register for remote notifications is registered with .Sound.

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

Here is a screenshot of my XCode project manager, showing where my custom sound is located.

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

The code that sends a Remote Notification from a firebase function:

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中注册代码的样子:

Here is what my register code looks like within my 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上读到的错误,但没有执行任何操作.如果有帮助,我可以在第六代iPad上运行iOS 12.1.4.

I have even tried rebooting my device because that was a bug that I read about on another Stack Overflow but that didn't do anything. If it helps I am running iOS 12.1.4 on a 6th generation iPad.

谢谢您的建议.

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

EDIT Just forgot, I have this in my info.plist file as well

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

推荐答案

您通过firebase发送的声音名称必须与扩展名为

your sound name that is sent through firebase have to match the name on the sound file with it's extension

还要确保在构建阶段就已在项目中正确实施了文件

also make sure that the files are correctly implemented in the project from build phases

关于默认声音,iOS 10之后不建议通过info.plist更改默认声音名称

regarding the default sound, Changing the default sound name through info.plist is deprecated after iOS 10

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

这篇关于在从FCM接收到iOS 12上的远程推送通知时播放自定义声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆