【发布时间】:2018-08-01 09:28:30
【问题描述】:
为了使用 Firebase 云消息服务,我实施了这些程序。 我设置了
FirebaseAppDelegateProxyEnabled 为 NO(swizzling 已禁用)
因为我想通过使用结构“data”而不是“notification”结构来自定义发送到设备的消息。 我可以从调试输出中看到,在这个回调中收到了一条消息:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
// I want to play a sound
// create a sound ID, in this case its the SMSReceived sound.
let systemSoundID: SystemSoundID = 1007 // file:// /System/Library/Audio/UISounds/sms-received1.caf
// to play sound
AudioServicesPlaySystemSound(systemSoundID)
if let title = userInfo["title"] as? NSString {
let myalert = MyAlert(title: "New message received!", message: title as String, preferredStyle: UIAlertControllerStyle.alert)
myalert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
myalert.addAction(UIAlertAction(title: "Open app", style: .default, handler: { _ in
print("The \"OK\" alert occured.")
}))
self.window?.rootViewController?.present(myalert, animated: true, completion: nil)
}// END if let title = alert["title"]
completionHandler(UIBackgroundFetchResult.newData)
}
在两种情况下(前后)都会加注。 前台一切正常,但后台没有声音和警报。 我很确定我错过了一些东西,但我不明白什么......也许我需要操作系统的一些“授权”来显示、提醒和播放声音?
编辑 1: 我实现了 UNUserNotificationCenterDelegate 来请求授权:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
// [START set_messaging_delegate]
Messaging.messaging().delegate = self
// [END set_messaging_delegate]
// Register for remote notifications. This shows a permission dialog on first run, to
// show the dialog at a more appropriate time move this registration accordingly.
// [START register_for_notifications]
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
// [END register_for_notifications]
if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
// show alert
let myalert = MyAlert(title: "New message!", message: "MyApp" as String, preferredStyle: UIAlertControllerStyle.alert)
myalert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
myalert.addAction(UIAlertAction(title: "Open MyApp", style: .default, handler: { _ in
print("The \"OK\" alert occured.")
}))
self.window?.rootViewController?.present(myalert, animated: true, completion: nil)
}
return true
}
【问题讨论】:
-
谢谢@Honey 的回答,我已经这样做了,你可以在 EDIT 1 中阅读......
-
@Honey 我没有回答你关于我为什么不创建内置系统(通知结构)的问题。只是因为当应用程序在后台时我无法播放声音!
-
"应用在后台时无法播放声音!"谁说的。如果我没记错的话,您可以创建一个推送通知并将声音包含到其有效负载中+将声音文件添加到应用程序包中。见here。(我删除了我之前的评论。这是不正确的)。如果您需要将声音放在前台,那么您的 wilPresentNotification 中也必须有
completionHandler([.alert, .sound])。见this tutorial
标签: ios swift notifications firebase-cloud-messaging