Summarize the response method of iOS receiving remote push

4482 단어
Clicking on iOS to receive remote push mainly involves the following five methods
(1) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
(2) - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
(3) - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
(4) - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
(5) - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

  • will be called when the app is started. launchOptions Saves the reason for the app to start. If the app is started by clicking on the notification bar, you can get the specific content of the notification at launchOptions
  • Will be called when a notification is received. It has been deprecated in the latest iOS 10 and is no longer recommended.
  • is a new method after iOS 7. It can be said that it is an upgraded version of 2. If the app supports iOS 7 at least, it is not necessary to add 2. Among them, completionHandler The parameter UIBackgroundFetchResult that this block can fill in is an enumeration value. It is mainly used to perform some operations in the background state, such as requesting data. After the operation is completed, the system must be notified that the acquisition is completed. The available results are

  • typedef NS_ENUM(NSUInteger, UIBackgroundFetchResult) {
        UIBackgroundFetchResultNewData,
        UIBackgroundFetchResultNoData,
        UIBackgroundFetchResultFailed
    }
    
    Indicates that new data is obtained (at this time, the system will take a screenshot of the current UI state and update the screenshot of your app in the App Switcher), no new data, and failed to obtain. However, the premise of the above operation is that Background Modes (push wake-up) has been checked in Remote notifications and the pushed message contains the content-available field.

  • is the UNUserNotificationCenterDelegate proxy method newly added in iOS 10. In the iOS 10 environment, this method will be called when clicking on the notification bar.
  • Also added in iOS 10. UNUserNotificationCenterDelegate The proxy method. Before iOS 10, if the app was in the foreground state and received a push, the notification bar would not give any prompts. If the developer needs to display the notification, he needs to 3 methods to extract the notification content display. In iOS 10, if the developer needs to display the notification in the foreground, the corresponding parameter can be passed in completionHandler in this method.

  • typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
        UNNotificationPresentationOptionBadge   = (1 << 0),
        UNNotificationPresentationOptionSound   = (1 << 1),
        UNNotificationPresentationOptionAlert   = (1 << 2),
    } 
    
    Summary: (minimum system environment is iOS 7)

  • When the program is closed, the push message is received, and the push message cannot be obtained by clicking the app icon. In the iOS 10 environment, clicking the notification bar will call methods 1 and 4. In the case of non-iOS 10, methods 1 and 3 will be called.
  • When the program is in the foreground and receives a push message, in the iOS 10 environment, if the pushed message contains the content-available field, execute methods 3 and 4, otherwise only execute 4. Method 3 will be executed in the case of non-iOS 10.
  • When the program is in the background and receives a push message, if Background Modes (push wakeup) has been checked in Remote notifications and the pushed message contains the content-available field, method 3 will be executed. Clicking the notification bar iOS 10 will execute method 4, non-iOS10 will execute method 3.
  • 좋은 웹페이지 즐겨찾기