iOS10 adaptive remote push function implementation code

  • 2020-11-18 06:30:20
  • OfStack

After the official version of iOS10 was released, various online articles adapted to XCode8 and iOS10 flew everywhere. However, there are few articles for remote push of iOS10. iOS10 has made a very big modification to push, and added UserNotifications Framework. Today, I will talk about the actual adaptation based on my own project.

1. Switch on Push Notifications in Capabilities

In XCode7, the switch here does not punch, and push can be used normally. However, in XCode8, the switch here must be turned on, otherwise an error will be reported:

Error Domain=NSCocoaErrorDomain Code=3000 "aps-ES24en" authorization string for application "UserInfo={NSLocalizedDescription= aps-ES28en" authorization string for application not found "ES23en-ES24en"}

When opened, the entitlements file is generated, where you can set APS Environment

2. Push registration

So first we introduce UserNotifications Framework,

import < UserNotifications/UserNotifications.h >
iOS10 has changed the method of registering push, here we need to set up the different versions separately. Modify the previous push Settings in the application didFinishLaunchingWithOptions method (I only implemented Settings above iOS8)


if (IOS_VERSION >= 10.0) {
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (!error) {
    DLog(@"request authorization succeeded!");
   }
  }];
 } else {
  if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
   //IOS8 To create UIUserNotificationSettings , and sets the display class type of the message 
   UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
 
   [application registerUserNotificationSettings:notiSettings];
  }
 }

3. UNUserNotificationCenterDelegate agent implementation

Processing push messages in iOS10 requires implementing two methods of UNUserNotificationCenterDelegate:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler

The first method is the callback method performed by App when it is in the foreground, and the second method is the callback method performed by App when it is in the background. Click the push message to enter the callback method performed by App.

In the previous push processing, the information was in userInfo parameter, but the new method shows that there is no such parameter. In fact, we can get userInfo as follows:


/// App Call back at the front desk 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
 NSDictionary *userInfo = notification.request.content.userInfo;
 [self handleRemoteNotificationForcegroundWithUserInfo:userInfo];
}
 
/// App Click push call in the background 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
 NSDictionary *userInfo = response.notification.request.content.userInfo;
 [self handleRemoteNotificationBackgroundWithUserInfo:userInfo];
}

After completing the above three steps, the push Settings for iOS10 are basically appropriate. To customize Notification Content or implement other NotificationAction refer to other articles. This is just an adaptation of iOS10.

This article has been organized into iOS push tutorial, welcome to learn to read.


Related articles: