亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS10全新推送功能實現代碼

2020-07-26 03:14:00
字體:
來源:轉載
供稿:網友

從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推送教程》,歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美性猛交xxxx免费看漫画| 亚洲激情在线观看| 亚洲国产古装精品网站| 日本老师69xxx| 精品少妇v888av| 97不卡在线视频| 国产福利精品视频| 久久精品久久久久久| 91精品视频网站| 日韩欧美aⅴ综合网站发布| 97色在线播放视频| 久久久久国产精品免费网站| 欧美整片在线观看| 日韩中文字幕网站| 欧美性猛交xxxx黑人| 国产精品在线看| 久久久久久久久久婷婷| 国产一区二区三区在线| 成人精品久久久| 欧美老妇交乱视频| 国模gogo一区二区大胆私拍| 久热精品视频在线观看一区| 91久久精品国产| 国产激情视频一区| 日本成熟性欧美| 日韩精品小视频| 97视频在线播放| 国产99久久精品一区二区永久免费| 国产精品视频在线观看| 成人精品一区二区三区电影免费| 日本高清不卡在线| 国产精品一香蕉国产线看观看| 亚洲一区二区久久久久久久| 欧美高清视频免费观看| 91高潮在线观看| 欧美尺度大的性做爰视频| 欧美成人第一页| 亚洲男人第一av网站| 国产精品久久久久久久久久| 国产精品永久免费| 欧美一级免费视频| 91精品国产高清久久久久久91| 91精品综合久久久久久五月天| 国产精品久久久精品| 国产一区二区三区精品久久久| 成人黄色av网站| 国产日韩视频在线观看| 成人国产亚洲精品a区天堂华泰| 欧美日韩精品中文字幕| 国产三级精品网站| 欧美精品videossex性护士| 亚洲精品久久久久中文字幕欢迎你| 亚洲精品自产拍| 亚洲欧美成人一区二区在线电影| 日韩综合视频在线观看| 日韩国产激情在线| 2019亚洲日韩新视频| 岛国av午夜精品| 久久久视频免费观看| 欧美日韩日本国产| 91av在线视频观看| 色无极亚洲影院| 欧美亚洲日本黄色| 国产精品免费在线免费| 欧美成人高清视频| 97久久精品人人澡人人爽缅北| 国产一区深夜福利| 精品欧美激情精品一区| 亚洲成**性毛茸茸| 疯狂欧美牲乱大交777| 九九九久久久久久| 亚洲欧美激情另类校园| 精品成人69xx.xyz| 欧美日韩精品在线视频| 最近2019年好看中文字幕视频| 欧美日韩精品在线视频| 在线亚洲午夜片av大片| 欧美亚洲另类激情另类| 欧美高跟鞋交xxxxhd| 国产成人啪精品视频免费网| 亚洲精品一区二区网址| 午夜精品久久久久久久99黑人| 91在线免费观看网站| 亚洲欧洲在线视频| 好吊成人免视频| 亚洲第一男人天堂| 亚洲欧美色婷婷| 欧美疯狂xxxx大交乱88av| 国产九九精品视频| 久久精品国产亚洲一区二区| 少妇高潮久久久久久潘金莲| 亚洲最大av网站| 高清在线视频日韩欧美| 国产精品999| 永久555www成人免费| 久久精品中文字幕电影| www.美女亚洲精品| 久久视频免费观看| 欧美精品在线观看91| 久久国产精品电影| 日韩欧美精品在线观看| 欧美精品www在线观看| 欧美激情乱人伦一区| 亚洲性日韩精品一区二区| 欧美精品在线免费| 欧美电影在线观看高清| 欧美国产日本在线| 国产视频福利一区| 欧美黑人xxxx| 欧美巨乳在线观看| 日日狠狠久久偷偷四色综合免费| 91免费精品视频| 欧美人在线视频| 亚洲精品福利免费在线观看| 日本免费久久高清视频| 日韩欧美中文免费| 精品香蕉一区二区三区| 国产午夜精品理论片a级探花| 91在线观看免费高清| 欧美激情中文字幕乱码免费| 国产欧美va欧美va香蕉在| 日韩在线一区二区三区免费视频| 尤物精品国产第一福利三区| 亚洲无限乱码一二三四麻| 自拍视频国产精品| 亚洲欧美激情一区| 久久伊人精品视频| 国产日本欧美在线观看| 国产精品国产三级国产专播精品人| 欧美猛少妇色xxxxx| 狠狠爱在线视频一区| 国产精品久久久久久影视| 亚洲成年人影院在线| 中文字幕亚洲综合| 亚洲片在线观看| 国产精品丝袜一区二区三区| 欧美乱大交做爰xxxⅹ性3| 一本色道久久88亚洲综合88| 日韩大片免费观看视频播放| 欧美日韩性视频| 久久久精品欧美| 国产欧美精品久久久| 日本乱人伦a精品| 久久久99免费视频| 久久福利视频网| 国产精品电影在线观看| 秋霞成人午夜鲁丝一区二区三区| 亚洲欧美在线第一页| 91国产一区在线| 国产亚洲xxx| 成人福利网站在线观看| 国产欧美 在线欧美| 日韩精品在线免费| 九九热视频这里只有精品| 欧美美女18p| 亚洲女人天堂成人av在线| 亚洲欧美中文另类| 日韩精品欧美激情| 久久久久久国产精品| 亚洲精品久久在线| 欧美激情精品久久久久久大尺度| 久久夜色精品国产| 97视频免费观看| 川上优av一区二区线观看|