2017-07-17 22 views
0

我试图实现我的应用程序火力点云的消息。didReceiveRemoteNotification前景火力云消息不叫于iOS

我使用下面的代码

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) 
     } 

,一切工作正常时,应用程序是在回地面注册的通知,但是当应用程序在前台,didReceiveRemoteNotification根本没有被调用。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    // Print message ID. 
    print("please help") 
    if let messageID = userInfo[gcmMessageIDKey] { 
     print("Message ID: \(messageID)") 
    } 

    // Print full message. 
    print(userInfo) 
    completionHandler(.newData) 
} 

任何人都知道这个可能的错误?谢谢!

编辑:我不需要看到的通知显示,当应用程序在前台,我只是想从通知

+0

这个完成处理程序根本不能调用吗? –

+0

是否声明UNUserNotificationCenterDelegate? – Mohit

回答

1

的iOS 10.x的处理消息有处理推送通知时,在一个新的功能前景,你应该实现这一个:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

    completionHandler(
     [UNNotificationPresentationOptions.alert, 
     UNNotificationPresentationOptions.sound, 
     UNNotificationPresentationOptions.badge]) 

} 
0

请执行以下方法来接收前景推送通知

对于iOS10,Swift3:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{ 
    completionHandler([.alert, .badge, .sound]) 
} 

对于iOS10,雨燕2.3:

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) 
{ 
    //Handle the notification 
    completionHandler(
     [UNNotificationPresentationOptions.Alert, 
     UNNotificationPresentationOptions.Sound, 
     UNNotificationPresentationOptions.Badge]) 
} 

尝试这一次,

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

    print("Recived: \(userInfo)") 

    completionHandler(.NewData) 

} 
+0

我在哪里实施此方法?顺便说一句,iOS 9及以下版本呢? – progammingBeignner

+0

在appdelegate中执行此操作。对于iOS9及以下版本,您不需要iOS9中的此方法 –

+0

检查是否正常工作。它应该工作我认为。 –

0

如果使用推送通知火力地堡的IOS 10 请添加此代码。希望能帮助你解决这个问题:

@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 message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print(userInfo) 
     completionHandler([]) 
    } 

    func userNotificationCenter(_ center: UNUserNotificationCenter, 
           didReceive response: UNNotificationResponse, 
           withCompletionHandler completionHandler: @escaping() -> Void) { 
     let userInfo = response.notification.request.content.userInfo 
     // Print message ID. 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     // Print full message. 
     print(userInfo) 
     completionHandler() 
    } 
} 

extension AppDelegate : FIRMessagingDelegate { 
    // Receive data message on iOS 10 devices while app is in the foreground. 
    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
     print(remoteMessage.appData) 
    } 
} 
相关问题