iOS 10 push notification when app terminated?

2019-05-18 01:04发布

I'm face the problem after upgraded to iOS 10 about push notification (I am using Swift3).

In normal case when application open or application still in background everything work as well (can receive push notification and update data as my logic).

But when application is terminated i can't handle push notification when application become active.

Here is my test case.

  1. Edit Scheme to Wait for executable to be launched.
  2. Double press home button and swipe application up.
  3. Run Xcode wait until "Wait for application to launch" shown.
  4. Test send push notification from server.
  5. Device received push notification.
  6. Start application from application icon.

After that application start and didFinishLaunchingWithOptions being called but launchOptions aways null so i can't handle push notification (But if i open application from notification in notification center or popup notification launchOptions is not null)

Does anybody has any idea to check this problem ?

Thank you in advance.

2条回答
唯我独甜
2楼-- · 2019-05-18 01:40

You need to open the application by tapping the push notification in the notification tray.

When you launch the application from the icon, launchOptions will be nil. Launching from the push notification will provide you launchOptions.

https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622921-application

查看更多
神经病院院长
3楼-- · 2019-05-18 01:50

Try this:

Add delegate on AppDelegate.

UNUserNotificationCenterDelegate

And..

//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("User Info = ",notification.request.content.userInfo)

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

//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("ActionIdentifier = ",response.actionIdentifier)
    print("User Info = ",response.notification.request.content.userInfo)

    completionHandler()
}
查看更多
登录 后发表回答