一:下面是本人在工做中的一點隨記,方便之後查用,其中須要您的App打開了background mode裏的Remote Notifications項,且須要APNS的payload裏指定content-available鍵爲1時html
1.AppDelegate+XPush.hios
#import "AppDelegate.h" @interface AppDelegate (XPush) @property (nonatomic, copy) NSString *token; @property (nonatomic, assign) NSInteger badgeNumber; @end
2.AppDelegate+XPush.mcookie
#import "AppDelegate+XPush.h" #import <objc/runtime.h> #import "HttpHelper.h" #ifndef __OPTIMIZE__ #define NSLog(...) NSLog(__VA_ARGS__) #else #define NSLog(...) {} #endif @implementation AppDelegate (XPush) static char tokenKey; NSInteger _badgeNumber; - (NSString *)token { return objc_getAssociatedObject(self, &tokenKey); } - (void)setToken:(NSString *)token { objc_setAssociatedObject(self, &tokenKey, token, OBJC_ASSOCIATION_COPY_NONATOMIC); } - (NSInteger)badgeNumber { NSLog(@"get---%ld",_badgeNumber); return _badgeNumber; } - (void)setBadgeNumber:(NSInteger)badgeNumber { NSLog(@"set---%ld",badgeNumber); _badgeNumber = badgeNumber; } //當app在後臺運行時,激活APP時會走這個方法,在這裏面裏能夠對推送消息作響應的處理 -(void)applicationDidBecomeActive:(UIApplication *)application{ // //把icon上的標記數字設置爲0, // application.applicationIconBadgeNumber = 0; // [[UIApplication sharedApplication] cancelAllLocalNotifications]; // NSLog(@"----self.badgeNumber----%ld",self.badgeNumber); // self.badgeNumber = 0; // NSLog(@"----把icon上的標記數字設置爲0----"); } - (id)init { /** If you need to do any extra app-specific initialization, you can do it here * -jm **/ NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; int cacheSizeMemory = 8 * 1024 * 1024; // 8MB int cacheSizeDisk = 32 * 1024 * 1024; // 32MB #if __has_feature(objc_arc) NSURLCache* sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; #else NSURLCache* sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease]; #endif [NSURLCache setSharedURLCache:sharedCache]; self = [super init]; //註冊消息推送 #ifdef __IPHONE_8_0 //Right, that is the point /*UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings];*/ if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } #else //register to receive notifications UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; #endif return self; } #ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //register to receive notifications註冊接收通知 [application registerForRemoteNotifications]; } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler { //handle the actions if ([identifier isEqualToString:@"declineAction"]){ } else if ([identifier isEqualToString:@"answerAction"]){ } } #endif //獲取DeviceToken成功 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]; // NSLog(@"deviceToken-%@",token); self.token = token; NSDictionary *dictionarry = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"PushConfig" ofType:@"plist"]]; NSString *appkey = [dictionarry objectForKey:@"APP_KEY"]; //1.請求url NSString *url = @"http://192.168.1/push"; //2.拼接請求參數 NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"appkey"] = appkey; params[@"token"] = token; NSLog(@"params:%@",params); //3.有網絡才發送請求 if([HttpHelper NetWorkIsOK]){ //發送請求,而且獲得返回的數據 [HttpHelper post:url RequestParams:params FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) { //傳回數據回調 NSLog(@"token發送成功"); }]; } } -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"error-%@",error); } //處理收到的消息推送 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"userInfo------------------:%@",userInfo); } -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"userInfo---------^^^^^---------:%@",userInfo); //遠程消息發送過來的時候,app正在運行 if (application.applicationState == UIApplicationStateActive) { // 轉換成一個本地通知,顯示到通知欄 UILocalNotification *localNotification = [[UILocalNotification alloc] init]; // 通知參數 localNotification.userInfo = userInfo; // 通知被觸發時播放的聲音 localNotification.soundName = UILocalNotificationDefaultSoundName; // 通知內容 localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; // 設置觸發通知的時間 localNotification.fireDate = [NSDate date]; // 執行通知註冊 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }else{ if (self.badgeNumber) { self.badgeNumber++; }else{ //self.badgeNumber = application.applicationIconBadgeNumber; self.badgeNumber = 1; } application.applicationIconBadgeNumber = self.badgeNumber; } } //應用每次被切換到前臺的時候 -(void)applicationWillEnterForeground:(UIApplication *)application{ NSLog(@"-----------WillEnterForeground----------"); //把icon上的標記數字設置爲0 application.applicationIconBadgeNumber = 0; self.badgeNumber = 0; } //進入後臺後 -(void)applicationDidEnterBackground:(UIApplication *)application{ NSLog(@"--------DidEnterBackground--------------"); //把icon上的標記數字設置爲0 NSLog(@"----application.applicationIconBadgeNumber----%ld",application.applicationIconBadgeNumber); application.applicationIconBadgeNumber = 0; [[UIApplication sharedApplication] cancelAllLocalNotifications]; NSLog(@"----self.badgeNumber----%ld",self.badgeNumber); self.badgeNumber = 0; NSLog(@"----把icon上的標記數字角標清零,取消全部本地通知----"); } @end
2.ios10推送更新,推薦幾個博客網絡
http://www.jianshu.com/p/2f3202b5e758app
http://www.open-open.com/lib/view/open1473734681248.htmlide
http://blog.csdn.net/qq_25527655/article/details/52597349post
http://blog.csdn.net/u012847940/article/details/51801078fetch
http://www.jianshu.com/p/f5337e8f336d這篇文章很好atom
3.借用網友博客裏面的一段內容url
iOS 10 中將通知相關的 API 都統一了,在此基礎上不少用戶定義的通知,而且能夠捕捉到各個通知狀態的回調.之前通知的概念是:你們想接受的提早作好準備,而後一下全兩分發,沒收到也無論了,也不關心發送者,如今的用戶通知作成了相似於網絡請求,先發一個request獲得response的流程,還封裝了error,能夠在各個狀態的方法中作一些額外的操做,而且能得到一些字段,好比發送者之類的.這個功能的頭文件是:#import
主要有如下文件:
#import <UserNotifications/NSString+UserNotifications.h> #import <UserNotifications/UNError.h> #import <UserNotifications/UNNotification.h> #import <UserNotifications/UNNotificationAction.h> #import <UserNotifications/UNNotificationAttachment.h> #import <UserNotifications/UNNotificationCategory.h> #import <UserNotifications/UNNotificationContent.h> #import <UserNotifications/UNNotificationRequest.h> #import <UserNotifications/UNNotificationResponse.h> #import <UserNotifications/UNNotificationSettings.h> #import <UserNotifications/UNNotificationSound.h> #import <UserNotifications/UNNotificationTrigger.h> #import <UserNotifications/UNUserNotificationCenter.h> #import <UserNotifications/UNNotificationServiceExtension.h>