【发布时间】:2017-02-14 10:31:19
【问题描述】:
我正在开发一个应该接收从 Firebase 控制台发送的推送通知的 iOS 应用。我正在使用 Swift 3 和 iOS 10。
根据 Firebase 文档的建议,我们必须在应用完成启动之前将委托对象分配给 UNUserNotificationCenter 对象以接收和显示通知,并将 FIRMessaging 对象分配给接收数据消息。
这已在didFinishLaunchingWithOptions 方法中完成。我按照所有步骤来配置 Firmessaging 以及 APN。
现在,当我从 Firebase 控制台发送消息时,我通过 applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) 方法接收它。
问题是我无法从remoteMessage.appData 字典中提取邮件正文。知道身体在remoteMessage.appData["notification"] 内。确实,指令
print(remoteMessage.appData)
打印
[AnyHashable("notification"): {
body = Hello Notifications;
e = 1;
}, AnyHashable("from"): 49679924394, AnyHashable("collapse_key"): com.company.app]
打印
remoteMessage.appData["notification"]
表演
{
body = Hello Notifications;
e = 1;
}
我试过了
remoteMessage.appData["notification"]["body"]
和
remoteMessage.appData["notification"].body
但它会导致语法错误。我无法提取身体以便在警报控制器中显示它。 appDelegate 的代码如下。
class AppDelegate: UIResponder, UIApplicationDelegate, FIRMessagingDelegate, UNUserNotificationCenterDelegate{
......
func application(_ application: UIApplication, didFinishLaunchingWithOptions, launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.isStatusBarHidden = true
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
if #available(iOS 10.0, *) {
let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: authOptions, completionHandler: {_ ,_ in })
application.registerForRemoteNotifications()
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
print(remoteMessage.appData)
print(remoteMessage.appData["notification"]!)
let alertController = UIAlertController(title: "Message from IOBird Developer Team", message: "?????? Body of the message ?????", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
}
alertController.addAction(OKAction)
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
【问题讨论】:
标签: ios firebase swift3 ios10 firebase-notifications