1.什麼是推送通知
2.推送通知的5種形式

3.推送通知的特點
4.遠程推送通知
5.遠程通知的實現基礎
6.遠程推送的實現原理


7.實現遠程推送功能的前提
8.實現遠程推送功能的步驟
9.遠程推送的具體實現過程
10.遠程推送過程中AppDelegate中所要響應的方法
下面的方法都是寫在AppDelegate.m文件中
/*
1.有一種打開,叫做點擊圖標後的打開
2.還有一種打開,叫做 點擊了 通知 之後的打開
當通過 點擊通知 這種方法打開應用程序,執行didFinishLaunching方法時,launchOptions
參數中,就存著通知發來的消息,也就是 類似於
didReceiveRemote方法中的那個userInfo
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIDevice currentDevice].systemVersion.doubleValue <= 8.0) {
//向服務器發請求,要注冊推送功能,以此獲取到服務器返回的deviceToken
//type 用來說明 支持的通知形式
//如 橫幅 聲音 角標
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
}else{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge| UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:settings];
//申請使用通知
[application registerForRemoteNotifications];
}
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(0, 40, 300, 200);
label.numberOfLines = 0;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:24];
label.backgroundColor = [UIColor blueColor];
label.text =[NSString stringWithFormat:@"%@",userInfo];
[self.window.rootViewController.view addSubview:label];
return YES;
}
//只要獲取到用戶同意,則服務器端返回deviceToken
//會自動執行下面的方法
//1417f54c c7f0adb0 48e3558f 2b8a8bad 0a6a5152 54af017e 32137cda 8cbdb9d0
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"%@",deviceToken);
}
/*
用戶點擊了通知,進入到應用程序中,需要捕獲到這個時機
從而決定這一次的進入應用程序,到底要顯示或執行什麼動作,下面的方法就會在點擊通知時自動調用
*/
/*
1.應用程序在前台時:通知到,該方法自動執行
2.應用程序在後台且沒有退出時:通知到,只有點擊了通知查看時,該方法自動執行
3.應用程序退出:通知到,點擊查看通知,不會執行下面的didReceive方法,而是只執行didFinishLauncing方法
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//NSLog(@"%@",userInfo);
//為了測試在應用程序退出後,該方法是否執行
//所以往第一個界面上添加一個label,看標簽是否會顯示一些內容
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(0, 250, 300, 200);
label.numberOfLines = 0;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:24];
label.backgroundColor = [UIColor grayColor];
label.text =[NSString stringWithFormat:@"%@",userInfo];
[self.window.rootViewController.view addSubview:label];
}
/*
此方法是新的用於響應遠程推送通知的方法
1.如果應用程序在後台,則通知到,點擊查看,該方法自動執行
2.如果應用程序在前台,則通知到,該方法自動執行
3.如果應用程序被關閉,則通知到,點擊查看,先執行didFinish方法,再執行該方法
4.可以開啟後台刷新數據的功能
step1:點擊target-->Capabilities-->Background Modes-->Remote Notification勾上
step2:在給APNs服務器發送的要推送的信息中,添加一組字符串如:
{"aps":{"content-available":"999","alert":"bbbbb.","badge":1}}
其中content-availabel就是為了配合後台刷新而添加的內容,999可以隨意定義
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(0, 250, 300, 200);
label.numberOfLines = 0;
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:24];
label.backgroundColor = [UIColor grayColor];
label.text =[NSString stringWithFormat:@"%@",userInfo];
[self.window.rootViewController.view addSubview:label];
//NewData就是使用新的數據 更新界面,響應點擊通知這個動作
completionHandler(UIBackgroundFetchResultNewData);
}
11.PushMeBaby
無法拿到證書的路徑:http://www.cnblogs.com/czq1989/p/5312146.html
demo:https://github.com/TigerCui/RemoteNotificationDemo.git