首页 > 解决方案 > iOS 为推送通知声音 FCM 启用声音

问题描述

我正在尝试为我的 Firebase 推送通知启用声音,但我不确定 App Delegate 中是否有我需要实现的代码,或者我的 index.js 中的代码是否错误。

我认为我需要在 AppDelegate 中导入与声音相关的内容,但我发现的所有实现推送通知的指南都只有基本代码,其中 [options] 包含唯一与通知声音相关的内容。index.js 代码:

var notification = {
    notification: {
        title: conversation.conversationName,
        body: user.username + ': ' + message.text,
        sound: 'default'
    },
    topic: topic
}

应用程序委托代码:在 didFinishLaunchingWithOptions 中调用的函数。

import UIKit
import Firebase
import UserNotifications

private func attemptRegisterForNotifications(application: UIApplication) {

        Messaging.messaging().delegate = self

        UNUserNotificationCenter.current().delegate = self

        let options: UNAuthorizationOptions = [.alert, .badge, .sound]

        UNUserNotificationCenter.current().getNotificationSettings { (settings) in

            if settings.authorizationStatus == .authorized {
                // Push notifications already authorized, do nothing
                print ("push notifications authorized")
            } else if settings.authorizationStatus == .notDetermined {
                // User hasn't specified notification status
                UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in
                    if let error = error {
                        print ("Failed to request authorization:", error)
                        return
                    }

                    guard granted else {return}
                    DispatchQueue.main.async {
                        application.registerForRemoteNotifications()
                    }
                })
            } else if settings.authorizationStatus == .denied {
                // User has denied notifications
                UNUserNotificationCenter.current().requestAuthorization(options: options, completionHandler: { (granted, error) in

                    if let error = error {
                        print ("Failed to request authorization:", error)
                        return
                    }

                    let alertController = UIAlertController(title: "Enable Push Notifications", message: "Enable push notifications for optimal chat experience", preferredStyle: .alert)
                    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
                        guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
                            return
                        }
                        if UIApplication.shared.canOpenURL(settingsUrl) {
                            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                            })
                        }
                    }
                    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
                    alertController.addAction(cancelAction)
                    alertController.addAction(settingsAction)
                    alertController.preferredAction = settingsAction
                    DispatchQueue.main.async {
                        self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
                    }
                })
            }
        }

    }

标签: javascriptiosfirebasefirebase-cloud-messaging

解决方案


推荐阅读