English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
From iOS8Starting from iOS version .0, the implementation of push features is constantly changing, and the functions are also increasing, iOS10Another push plugin development has emerged (see the last figure), no more words, let's go straight to the code:
#import <UserNotifications/UserNotifications.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after the application launch. /* In the case of the APP not running and clicking the push message, iOS10Abandon the use of UIApplicationLaunchOptionsLocalNotificationKey, use the UNUserNotificationCenterDelegate method didReceiveNotificationResponse:withCompletionHandler: to get local push notifications */ // NSDictionary *localUserInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; // if (localUserInfo) { // NSLog(@"localUserInfo:%@",localUserInfo); // //The APP is not running, click the push message // } NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteUserInfo) { NSLog(@"remoteUserInfo:%@",remoteUserInfo); //The APP is not running, click the push message, iOS10is still the same as before, get here } [self registerNotification]; return YES; }
Register the change of push method:
New library #import <UserNotifications/UserNotifications.h> Push single column UNUserNotificationCenter and other APIs
- (void)registerNotification{ /* identifier: The action identifier, used to identify which action is called when the proxy method is invoked. title: The name of the action. UIUserNotificationActivationMode: Whether the action opens the APP. authenticationRequired: Whether it is necessary to unlock. destructive: This determines the button display color, YES means the button will be red. behavior: Clicking the button text input, whether the keyboard pops up */ UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action"1" title:@"Strategy"1behavior1" options:UNNotificationActionOptionForeground]; /*iOS9Implementation method UIMutableUserNotificationAction * action1 [[UIMutableUserNotificationAction alloc] init]; action1.identifier = @"action"}1"; action1.title=@"Strategy1behavior1"; action1.activationMode = UIUserNotificationActivationModeForeground; action1.destructive = YES; */ UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action"2" title:@"Strategy"1behavior2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"textInputButtonTitle" textInputPlaceholder:@"textInputPlaceholder"]; /*iOS9Implementation method UIMutableUserNotificationAction * action2 [[UIMutableUserNotificationAction alloc] init]; action2.identifier = @"action"}2"; action2.title=@"Strategy1behavior2"; action2.activationMode = UIUserNotificationActivationModeBackground; action2.authenticationRequired = NO; action2.destructive = NO; action2.behavior = UIUserNotificationActionBehaviorTextInput;//Clicking the button text input, whether the keyboard pops up */ UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"Category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[@"action1",@"action2] options:UNNotificationCategoryOptionCustomDismissAction]; // UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init]; // category1.identifier = @"Category1"; // [category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)]; UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action"3" title:@"Strategy"2behavior1" options:UNNotificationActionOptionForeground]; // UIMutableUserNotificationAction * action3 [[UIMutableUserNotificationAction alloc] init]; // action3.identifier = @"action"}3"; // action3.title=@"Strategy2behavior1"; // action3.activationMode = UIUserNotificationActivationModeForeground; // action3.destructive = YES; UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action"4" title:@"Strategy"2behavior2" options:UNNotificationActionOptionForeground]; // UIMutableUserNotificationAction * action4 [[UIMutableUserNotificationAction alloc] init]; // action4.identifier = @"action"}4"; // action4.title=@"Strategy2behavior2"; // action4.activationMode = UIUserNotificationActivationModeBackground; // action4.authenticationRequired = NO; // action4.destructive = NO; UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"Category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[@"action3",@"action4] options:UNNotificationCategoryOptionCustomDismissAction]; // UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init]; // category2.identifier = @"Category2"; // [category2 setActions:@[action4,action3] forContext:(UIUserNotificationActionContextDefault)]; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]]; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) { }] /*iOS9Implementation method UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; */ [[UIApplication sharedApplication] registerForRemoteNotifications]; [UNUserNotificationCenter currentNotificationCenter].delegate = self; }
Change of delegate method:
Some local and remote push notifications callbacks are placed in the same delegate method
#pragma mark - - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{ NSLog(@"didRegisterUserNotificationSettings"); } - (void)application:(UIApplication *application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *deviceToken NS_AVAILABLE_IOS(3_0){ NSLog(@"deviceToken:%@",deviceToken); NSString *deviceTokenSt = [[[[deviceToken description stringByReplacingOccurrencesOfString:@"<" withString:@""]" stringByReplacingOccurrencesOfString:@">" withString:@""]" stringByReplacingOccurrencesOfString:@" " withString:@""];" NSLog(@"deviceTokenSt:%@",deviceTokenSt); } - (void)application:(UIApplication *application didFailToRegisterForRemoteNotificationsWithError:(NSError *error NS_AVAILABLE_IOS(3_0){ NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error); } /*iOS9Usage Method - (void)application:(UIApplication *application didReceiveRemoteNotification:(NSDictionary *userInfo NS_DEPRECATED_IOS(3_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] for user visible notifications and -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] for silent remote notifications"){ } */ - (void)userNotificationCenter:(UNUserNotificationCenter *center willPresentNotification:(UNNotification *notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ NSLog(@"willPresentNotification:%@",notification.request.content.title);}} // This is the real place where interaction needs to be handled // Get the data carried by the notification NSString *notMess = [notification.request.content.userInfo objectForKey:@"aps"]; } - (void)userNotificationCenter:(UNUserNotificationCenter *center didReceiveNotificationResponse:(UNNotificationResponse *response withCompletionHandler:(void(^)())completionHandler{ //When the app is not running, receiving server push messages, the message will have a quick reply button when pulling down the message. The method called after clicking the button is determined by the identifier. NSString *notMess = [response.notification.request.content.userInfo objectForKey:@"aps"]; NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title); // response.notification.request.identifier } //Remote push APP is in the foreground - (void)application:(UIApplication *application didReceiveRemoteNotification:(NSDictionary *userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"didReceiveRemoteNotification:%@",userInfo); } /* - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *identifier forRemoteNotification:(NSDictionary *userInfo completionHandler:(void(^)())completionHandler NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED { } */ /* // Local notification callback function, called when the application is in the foreground - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_DEPRECATED_IOS(4_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED{ NSLog(@"didReceiveLocalNotification:%@",notification.userInfo); // This is the real place where interaction needs to be handled // Get the data carried by the notification NSString *notMess = [notification.userInfo objectForKey:@"aps"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Local Notification (Foreground)" message:notMess delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; // Update the displayed badge count NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber; badge--; badge = badge >= 0 &63; badge : 0; [UIApplication sharedApplication].applicationIconBadgeNumber = badge; // When no longer need to push notifications, you can cancel the push. [FirstViewController cancelLocalNotificationWithKey:@"key"]; } - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler NS_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]") __TVOS_PROHIBITED { // NSLog(@"%@----%@",identifier,notification); completionHandler();//After processing the message, be sure to call this code block at the end } */
There is also push plugin development: similar to iOS tody widget plugin development
This article has been organized into 'iOS Push Tutorial', welcome to learn and read.
That's all for this article. Hope it helps everyone's learning and also hope everyone will support the Yelling Tutorial.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, the website will immediately delete the infringing content.