你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS10全新推送功效完成代碼

iOS10全新推送功效完成代碼

編輯:IOS開發綜合

從IOS8.0開端推送功效的完成在赓續轉變,功效也在赓續增長,IOS10又出來了一個推送插件的開辟(見最初圖),空話不多說直接上代碼: 

#import <UserNotifications/UserNotifications.h>
 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Override point for customization after application launch.
 
 /* APP未啟動,點擊推送新聞的情形下 IOS10拋棄UIApplicationLaunchOptionsLocalNotificationKey,應用署理UNUserNotificationCenterDelegate辦法didReceiveNotificationResponse:withCompletionHandler:獲得當地推送
 */
// NSDictionary *localUserInfo = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
// if (localUserInfo) {
// NSLog(@"localUserInfo:%@",localUserInfo);
// //APP未啟動,點擊推送新聞
// }
 NSDictionary *remoteUserInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
 if (remoteUserInfo) {
 NSLog(@"remoteUserInfo:%@",remoteUserInfo);
 //APP未啟動,點擊推送新聞,iOS10下照樣跟之前一樣在此獲得
 }
 [self registerNotification];
 return YES;
}
 

注冊推送辦法的轉變:

新增庫 #import <UserNotifications/UserNotifications.h>  推送單列UNUserNotificationCenter 等API 

- (void)registerNotification{
 /*
 identifier:行動標識符,用於挪用署理辦法時辨認是哪一種行動。
 title:行動稱號。
 UIUserNotificationActivationMode:即行動能否翻開APP。
 authenticationRequired:能否須要解鎖。
 destructive:這個決議按鈕顯示色彩,YES的話按鈕會是白色。
 behavior:點擊按鈕文字輸出,能否彈出鍵盤
 */
 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"戰略1行動1" options:UNNotificationActionOptionForeground];
 /*iOS9完成辦法
 UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
 action1.identifier = @"action1";
 action1.title=@"戰略1行動1";
 action1.activationMode = UIUserNotificationActivationModeForeground;
 action1.destructive = YES;
 */
 
 UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action2" title:@"戰略1行動2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"textInputButtonTitle" textInputPlaceholder:@"textInputPlaceholder"];
 /*iOS9完成辦法
 UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
 action2.identifier = @"action2";
 action2.title=@"戰略1行動2";
 action2.activationMode = UIUserNotificationActivationModeBackground;
 action2.authenticationRequired = NO;
 action2.destructive = NO;
 action2.behavior = UIUserNotificationActionBehaviorTextInput;//點擊按鈕文字輸出,能否彈出鍵盤
 */
 
 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:@"action3" title:@"戰略2行動1" options:UNNotificationActionOptionForeground];
 // UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
 // action3.identifier = @"action3";
 // action3.title=@"戰略2行動1";
 // action3.activationMode = UIUserNotificationActivationModeForeground;
 // action3.destructive = YES;
 
 UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action4" title:@"戰略2行動2" options:UNNotificationActionOptionForeground];
 // UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init];
 // action4.identifier = @"action4";
 // action4.title=@"戰略2行動2";
 // 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) {
 NSLog(@"completionHandler");
 }];
 /*iOS9完成辦法
 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];

 [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
 */
 [[UIApplication sharedApplication] registerForRemoteNotifications];
 
 
 [UNUserNotificationCenter currentNotificationCenter].delegate = self;
}

署理辦法的轉變:

 一些當地和長途推送的回調放在了統一個署理辦法

#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);
}

/*iOS9應用辦法
- (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);
 
 // 這裡真實須要處置交互的處所
 // 獲得告訴所帶的數據
 NSString *notMess = [notification.request.content.userInfo objectForKey:@"aps"];
 
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
 //在沒有啟動本App時,收到辦事器推送新聞,下拉新聞會有快捷答復的按鈕,點擊按鈕後挪用的辦法,依據identifier來斷定點擊的哪一個按鈕
 NSString *notMess = [response.notification.request.content.userInfo objectForKey:@"aps"];
 NSLog(@"didReceiveNotificationResponse:%@",response.notification.request.content.title);
// response.notification.request.identifier
}

//長途推送APP在前台
- (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
{
 
}
*/
/*
// 當地告訴回調函數,當運用法式在前台時挪用
- (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);
 
 
 // 這裡真實須要處置交互的處所
 // 獲得告訴所帶的數據
 NSString *notMess = [notification.userInfo objectForKey:@"aps"];
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"當地告訴(前台)"
    message:notMess
    delegate:nil
   cancelButtonTitle:@"OK"
   otherButtonTitles:nil];
 [alert show];
 
 // 更新顯示的徽章個數
 NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
 badge--;
 badge = badge >= 0 ? badge : 0;
 [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
 
 // 在不須要再推送時,可以撤消推送
 [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
{
 //在非本App界面時收到當地新聞,下拉新聞會有快捷答復的按鈕,點擊按鈕後挪用的辦法,依據identifier來斷定點擊的哪一個按鈕,notification為新聞內容
 NSLog(@"%@----%@",identifier,notification);
 completionHandler();//處置完新聞,最初必定要挪用這個代碼塊
}
*/

 還有推送插件開辟: 相似iOS tody widget插件開辟

 

本文已被整頓到了《iOS推送教程》,迎接年夜家進修浏覽。

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。

【iOS10全新推送功效完成代碼】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved