【问题标题】:Push Notification not showing in iOS 10推送通知未在 iOS 10 中显示
【发布时间】:2016-10-07 10:01:27
【问题描述】:

如何在 iOS 10 中获取通知?在以前的版本中,我可以在func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 收到通知。

在 iOS 10 中,Apple 引入了UNNotificationSettings。我可以通过func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) 获得 FCM。

但是,我的手机无法在后台收到通知。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    FIRApp.configure()

    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
            authOptions,
            completionHandler: {_,_ in })

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }


    return true
}

@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate, FIRMessagingDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)

}


// Receive data message on iOS 10 devices.
func applicationReceivedRemoteMessage(remoteMessage: FIRMessagingRemoteMessage) {
    print("%@", remoteMessage.appData)
}
}

【问题讨论】:

  • 我不确定这是否有帮助,但在 iOS 10 中添加了一些新要求,这意味着我必须检查兼容性屏幕,它在我必须检查的推送通知下向我显示警告添加具有以下属性的权利文件:APS Environment = development。您是否收到任何错误或警告?
  • 是的,已经是了。此外,我没有收到任何错误或警告。只是没有显示通知。
  • 是否为iOS10注册了deviceToken?
  • 在iOS10 Notifications后添加application.registerForRemoteNotifications()
  • 我有同样的问题,即使 application.registerForRemoteNotifications() 在 else {..} 我从 Firebase 控制台或任何其他 APN 发送推送通知时可以收到推送通知,但不是我将其作为 POST 发送到 fcm.googleapis.com/fcm/send 。当我通过 POST 发送它时,我可以在 xCode 控制台中看到它们,但无法将它们显示为警报通知(即使优先级 =“高”)

标签: swift firebase ios10


【解决方案1】:

你必须移动这个方法

application.registerForRemoteNotifications()

到上面的return语句。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?)
-> Bool {
    FIRApp.configure()

    if #available(iOS 10.0, *) {
        let authOptions : UNAuthorizationOptions = [.Alert, .Badge, .Sound]
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(
            authOptions,
            completionHandler: {_,_ in })

        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        // For iOS 10 data message (sent via FCM)
        FIRMessaging.messaging().remoteMessageDelegate = self

    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        application.registerUserNotificationSettings(settings)        
    }

    application.registerForRemoteNotifications()

   NotificationCenter.default.addObserver(self,
                                           selector: #selector(self.tokenRefreshNotification),
                                           name: .firInstanceIDTokenRefresh,
                                           object: nil)

    return true
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2017-05-24
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多