English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

iOS10 Code Implementation for Adapting Remote Push Function

iOS10After the official version is released, there are various adaptations for XCode on the Internet8and iOS10articles are everywhere. But for iOS10There are not many articles on adapting remote push notifications. iOS10The modification for push notifications is quite significant, with the addition of the UserNotifications Framework. Today, let's talk about the actual adaptation situation combined with my own project. 

Firstly, turn on the Push Notifications switch in the Capabilities

In XCode7In this case, if the switch is not turned on, push notifications can still be used normally, but in XCode8In this case, the switch must be turned on, otherwise an error will occur:

Error Domain=NSCocoaErrorDomain Code=3000 "The application's "aps-authorization string for the "environment": "UserInfo={NSLocalizedDescription=The application's "aps-authorization string for the "environment"}

After opening, an entitlements file will be generated, where you can set the APS Environment 

Secondly, the registration of push notifications

Firstly, introduce the UserNotifications Framework,

import <UserNotifications/UserNotifications.h>
iOS10Modified the method of registration for push notifications, and here we need to set for different versions separately. In the application didFinishLaunchingWithOptions method, modify the previous push notification settings (I have only implemented iOS8the above settings) { 

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!");
   }
  ;
 }
  if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
   //IOS8, create UIUserNotificationSettings and set the display type of the message
   UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
   [application registerUserNotificationSettings:notiSettings];
  }
 }

3. Implementation of UNUserNotificationCenterDelegate Proxy

in iOS10To handle push notifications, it is necessary to implement 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 executed when the app receives a push notification when the app is in the foreground, and the second one is the callback method executed after the app is opened by clicking the push notification in the background.

In the previous method of handling push notifications, the information is in the userInfo parameter, while in the new method, it seems that there is no such parameter. In fact, we can still get userInfo as follows: 

/// callback when the app is in the foreground
- (void)userNotificationCenter:(UNUserNotificationCenter *center willPresentNotification:(UNNotification *notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
 NSDictionary *userInfo = notification.request.content.userInfo;
 [self handleRemoteNotificationForcegroundWithUserInfo:userInfo];
}
/// App is clicked on push when 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 of setting, for iOS10The basic push settings have been adapted. If you want to customize Notification Content or implement other NotificationAction, please refer to other articles. This is just an adaptation to iOS10Adaptation.

This article has been organized into 'iOS Push Tutorial', welcome to study and read.

That's all for this article. I hope it will be helpful to everyone's study, and I also hope everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like