0

I already imported

import UserNotifications

and connected delegate:

UNUserNotificationCenterDelegate

and set delegates as self:

UNUserNotificationCenter.currentNotificationCenter().delegate = self
FIRMessaging.messaging().remoteMessageDelegate = self

and finally I have these functions:

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

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    //Handle the notification
    print("User Info = ",response.notification.request.content.userInfo)
    completionHandler()
}

but I do not get on my log any prints when I'm testing on a real device and in the background mode.

What is the problem?

internal func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    print(userInfo)

    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {        
    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print(userInfo)
}
J. Doe
  • 563
  • 1
  • 9
  • 20
  • You must have to debug your code using breakpoint. If this method didReceiveRemoteNotification is not called. Then you have to check backend. – Amanpreet Dec 08 '16 at 12:04
  • it debugs only if I'm in app. But when I'm in the background mode, I receive the notification, but cannot handle it @Amanpreet – J. Doe Dec 08 '16 at 12:13
  • Please check the answer below. And why are you use two methods of "didReceiveRemoteNotification" ? – Amanpreet Dec 08 '16 at 12:24

2 Answers2

2

Please try with following code. You just need to save data.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> ())
{
     let info : NSDictionary! = userInfo as! NSDictionary

     if info != nil
      {
          let aps = info["aps"] as? NSDictionary
          UserDefaults.standard.set(aps, forKey: "aps")
       }
   }

And use the user defaults where you want to use.

Edvinas
  • 145
  • 1
  • 9
Amanpreet
  • 1,301
  • 3
  • 12
  • 29
  • Check out http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10 – Amanpreet Dec 09 '16 at 04:57
  • I have updated my answer. You can check here http://stackoverflow.com/questions/41035878/how-can-i-store-push-notification-alert-message-in-userdefault/41037919#41037919 – Amanpreet Dec 09 '16 at 07:18
  • surely you must call the completion handle3r ?? – Fattie Mar 07 '20 at 21:45
0
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  let info = userInfo as! [String : AnyObject]
  let aps = info["aps"] as? NSDictionary
  print(aps)
 }
urvashi bhagat
  • 1,123
  • 12
  • 15