APNS(Apple Push Notification Services)蘋果專門的推送服務器 接收咱們本身應用服務器須要被推送的消息 而後推送到咱們的手機 手機通知咱們的應用程序 php
註冊的大概流程:html
1 設備須要向APNS服務器註冊ios
2 註冊成功後返回device_token值數組
3 將這個token值發送給咱們本身的服務器 服務器
4 有須要推送的消息時 本身的服務器將消息按必定的格式打包 結合token值發送給APNS服務器 網絡
5 因爲App與APNS維持一個基於TCP的長鏈接 APNS將消息推送到咱們的手機上app
推送大體流程學習
1 你的APP支持推送 APP鏈接網絡後會鏈接APNS 鏈接過程當中 APNS會驗證devices_token 鏈接成功後維持一個長鏈接測試
2 咱們的服務器接收到須要推送的消息後將消息和設備的device_token一塊兒打包發送給APNS服務器url
3 APNS服務器接收到消息發送到指定device_token設備
4 設備接收到消息 通知咱們的App
打包的消息結構
觀看大神寫的詳情:http://www.cnblogs.com/taintain1984/p/3723324.html
1 本地推送
應用本地提醒用戶事件
// 註冊本地推送消息
+ (void)registerLocalNotification:(NSInteger)alertTime {
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 設置觸發通知的時間
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate);
notification.fireDate = fireDate;
// 時區
notification.timeZone = [NSTimeZone defaultTimeZone];
// 設置重複的間隔
notification.repeatInterval = kCFCalendarUnitSecond;
// 通知內容
notification.alertBody = @"該起牀了...";
notification.applicationIconBadgeNumber = 1;
// 通知被觸發時播放的聲音
notification.soundName = UILocalNotificationDefaultSoundName;
// 通知參數
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"開始學習iOS開發了" forKey:@"key"];
notification.userInfo = userDict;
// ios8後,須要添加這個註冊,才能獲得受權
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重複提示的單位,能夠是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重複提示的單位,能夠是天、周、月
notification.repeatInterval = NSCalendarUnitMonth;
}
// 執行通知註冊
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
// 接收到推送消息執行的方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"noti:%@",notification);
// 這裏真實須要處理交互的地方
// 獲取通知所帶的數據
NSString *notMess = [notification.userInfo objectForKey:@"key"];
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;
}
// 在Appdelegate 中調用註冊推送消息的方法
[AppDelegate registerLocalNotification:10];
想要取消推送 須要調用
// 取消某個本地推送通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
// 獲取全部本地通知數組
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) {
// 根據設置通知參數時指定的key來獲取通知參數
NSString *info = userInfo[key];
// 若是找到須要取消的通知,則取消
if (info != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
break;
}
}
}
}
大神博客介紹:http://blog.csdn.net/woaifen3344/article/details/44302635
2 服務器推送
根據APP ID建立推送證書 轉化成p12 再轉成pem給後臺用 後臺推送的時候要把這個證書和push.php放在一塊兒 後臺使用push.php放在一塊兒 後臺使用push.php推送的時候要讀取這個證書
建立一個測試證書
Appdelegate註冊推送服務 而且經過代理方法的回調來獲取註冊成功的token值或者註冊失敗的信息(註冊須要設置聯網才能夠 token能夠理解爲用戶手機的惟一標識和應用程序的App ID組合生成的一個惟一標識,用來標識是哪一個手機上的App token不是一直不變的 好比測試和發佈的程序返回的token是不同的)
把token值給服務器 這樣服務器就會接受每個贊成了使用推送服務的用戶的token 能夠保存爲一張表 當後臺服務器須要發送消息的時候就能夠從這張表裏面找token後去發送消息
if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0) {
// 1 向用戶發送請求 去註冊推送服務
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; //彈框 角標 聲音
// 2 把推送設置配置給咱們的應用程序
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];//iOS8.0
// 3 註冊推送服務
[[UIApplication sharedApplication] registerForRemoteNotifications];
}else{
// iOS8.0前的方法
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
// 當前應用程序正在運行時候 接收到推送消息會執行的這個方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"%@",userInfo);
_commentLabel.text = [userInfo objectForKey:@"aps"][@"alert"];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"註冊遠程推送服務失敗:%@",error);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"token :%@",deviceToken);
// 正常狀況下 會在這個地方發送請求 把這個token返回給本身的服務器
}
// 請求本地服務器數據
NSString *urlStr=[NSString stringWithFormat:@"http://10.80.6.165/push.php?deviceName=%@&message=%@",_message,_messageTF.text];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"ERROR:%@",connectionError);
}else{
NSLog(@"請求成功");
}
}];
//詳情介紹: http://blog.csdn.net/shenjie12345678/article/details/41120637