2017-10-15 113 views
1

我知道類似的問題已經被問了很多次。但它仍然是閱讀那些線程後非常混亂給我,UNUserNotificationCenter在iOS版推出後,尤其是10我應該使用哪種方法處理iOS遠程通知?

的官方文檔中提到3種方法在那裏我可以處理遠程通知:

  1. 實施userNotificationCenter:willPresentNotification:withCompletionHandler:來處理當應用程序處於前景時的通知。
  2. 執行userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:當應用程序在後臺或未運行。
  3. 但是文檔中還提到:在iOS和tvOS中,系統將通知有效載荷傳遞給應用程序委託的application:didReceiveRemoteNotification:fetchCompletionHandler:方法。

所以,

  • 要處理遠程通知時應用程序在後臺/無效,應我把我的代碼的應用程序委託方法在3或2的通知中心委託?由於UNUserNotificationCenter僅適用於iOS> 10,我應該編寫不同的代碼來處理每個案例嗎?
  • 關於1,它僅在iOS 10之後提供。如何在iOS 10之前的應用程序在前臺運行時處理遠程通知?

而且,更令人困惑的是:如果應用程序在後臺,則何時調用委託方法:何時收到通知消息?或者當用戶點擊通知?

相關:iOS push notification: how to detect if the user tapped on notification when the app is in background?

回答

1

iOS的10及更高版本:

1)userNotificationCenter willPresent通知:一般用來決定何時用戶已經在應用程序中,並通知到達該怎麼辦。您可能會在應用程序內觸發遠程通知。在他點擊遠程通知後,方法2(didReceive響應)被調用。

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

//Handle push from foreground 
//When a notification arrives and your user is using the app, you can maybe notify user by showing a remote notification by doing this 
completionHandler([.alert, .badge, .sound]) 

//To print notification payload: 
print(notification.request.content.userInfo) 

} 

2)userNotificationCenter didReceive響應:一般用於將用戶重定向到用戶敲擊該通知後該應用的特定屏幕。

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

//Handle push from background or closed (or even in foreground) 
//This method is called when user taps on a notification 

//To print notification payload: 
print(response.notification.request.content.userInfo) 

} 

下面的iOS 10:

3)應用didReceiveRemoteNotification

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

    //To print notification payload 
    print(userInfo) 

    if #available(iOS 10.0, *) { 

    } 
    else { 

     //Handle remote notifications for devices below iOS 10 

     if application.applicationState == .active { 
     //app is currently in foreground 

     } 
     else if application.applicationState == .background { 
     //app is in background 

     } 
     else if application.applicationState == .inactive { 
     //app is transitioning from background to foreground (user taps notification) 

     } 
    } 
} 

4)應用didFinishLaunchingWithOptions launchOptions:其被放置裝置之下的iOS 10的唯一情形當應用程序關閉時,用戶點擊啓動應用程序的通知。您必須檢查以下方法。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
//To print notification payload: 
    if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] { 
     print(notification) 
    } 
} 

LaunchOptions是指示該應用是 推出的原因(如果有的話)的字典。在用戶直接啓動應用程序的情況下,此字典的內容可能爲空,如 。

現在回答你的問題,

1)處理遠程通知時應用程序在後臺/無效,你必須添加代碼方法2(userNotificationCenter didReceive響應)與設備iOS 10及以上版本。另外,對於iOS 10以下的設備,您必須使用方法3(應用程序didReceiveRemoteNotification)。

2)要在iOS 10之前的應用程序在前臺運行時處理遠程通知,請使用方法3活動狀態。

相關問題