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

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

问题描述

我正在尝试在应用完全关闭或在后台收到 REMOTE 通知时播放自定义声音,但我尝试过的一切都没有奏效.>

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

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

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

userData.tokens.forEach(async token => {console.log("令牌:"+令牌);常量消息 = {数据: {类型:类型"},安卓: {优先级:高"},通知: {"title": "邮件标题","sound": "customSound.caf"},aps:{标题:{apns-priority":10"},有效载荷:{应用程序:{声音:customSound.caf"},声音:customSound.caf"}},令牌:令牌};控制台日志(消息);等待 admin.messaging().send(message);});

以下是我的 AppDelegate 中的注册代码:

if #available(iOS 10.0, *) {//对于 iOS 10 显示通知(通过 APNS 发送)UNUserNotificationCenter.current().delegate = selfMessaging.messaging().delegate = self} 别的 {让设置:UIUserNotificationSettings = UIUserNotificationSettings(类型:[.alert,.badge,.sound],类别:nil)application.registerUserNotificationSettings(settings)}application.registerForRemoteNotifications()

我什至尝试重新启动我的设备,因为这是我在另一个 Stack Overflow 上读到的一个错误,但没有做任何事情.如果有帮助,我将在第 6 代 iPad 上运行 iOS 12.1.4.

感谢您的任何建议.

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

UILocalNotificationDefaultSoundName<string>OnceAgainNotification.caf</string>

解决方案

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

另外,确保文件从构建阶段就在项目中正确实现.

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

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

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.

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.

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

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);
});

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()

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.

Thanks for any suggestions.

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

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

解决方案

Your sound name that is sent through firebase has to match the name of the sound file with it's extension.

Also, make sure that the files are correctly implemented in the project from build phases.

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

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

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

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