【发布时间】:2018-05-08 12:21:43
【问题描述】:
当应用程序处于后台时,我已成功收到通知,但在 iOS 11 中未调用通知回调方法 didReceiveRemoteNotification 以使用 Swift4。当应用程序在后台收到通知时,我想获取通知有效负载数据。我正在使用 firebase 控制台发送通知。当应用打开时,会调用 willPresent。但是在后台没有调用任何方法 我正在使用代码:-
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any])
{
print(userInfo)
saveNotificationDetail(userInfo: userInfo)
}
func notificationIntegrate(application: UIApplication)
{
FirebaseApp.configure()
Messaging.messaging().shouldEstablishDirectChannel = true
Messaging.messaging().delegate=self
// [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]
// Add observer for InstanceID token refresh callback.
NotificationCenter.default
.addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotification),
name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print full message.
print(userInfo)
saveNotificationDetail(userInfo: userInfo)
completionHandler(UNNotificationPresentationOptions.alert)
// Change this to your preferred presentation option
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
{
let userInfo = response.notification.request.content.userInfo
print(userInfo)
}
}
【问题讨论】:
-
按照这里提到的步骤..medium.com/posts-from-emmerge/…
标签: ios swift firebase notifications firebase-cloud-messaging