iOS为推送通知声音FCM启用声音

时间:2018-11-11 00:26:24

标签: javascript ios firebase firebase-cloud-messaging

我正在尝试为我的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)
                    }
                })
            }
        }

    }

0 个答案:

没有答案