iOS - APNs源生推送

目录
0.各版本推送特性
1.证书配置
2.后台配置APNs格式
3.xcode项目配置推送
4.参考资料

正文
0.各版本推送特性
APNs Apple Push Notification service (APNs)
推送字段长度
很显著的变化是从iOS8开始Apple对消息字节有很大的扩容

推送内容为字节=(x)个字节 - 45个设备Token/默认占位标示符
x=iOS8&&x=iOS9                4KB

推送通知样式

系统版本         显示样式
iOS8以前        只有3个地方展示,点击进入应用
iOS8版本            提供快捷处理Actions
iOS9版本            提供快捷回复TextInput
  1. 证书配置
    这方面的资料有很多,优秀的博文也很多,按照下面接个配置即可。
http://blog.csdn.net/showhilllee/article/details/8631734 [是对raywenderlich的一个翻译版本,需要注意的是这个版本还是开发者中心改版之前的一个版本,但大体流程没有问题]
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
  1. 后台配置APNs格式
 /** 
{ 
  "aps":
    { 
      "alert":"//此处有两个服务器需要选择,如果是开发测试用,选择第二名sandbox的服务器并使用Dev的pem证书,如果是正是发布,使用Product的pem并选用正式的服务器", //消息首页展示内容
      "content-available":1,//允许iOS前端挂起时获取数据
      "badge":10, //icon上未读消息标示个数 
      "sound":"default", //推送听到的铃声 
      "data":{         //推送消息主体,供程序启动时做相应处理 
      "tid":1000001, //通知Id 做设备打开通知上报数据使用
      "id":101, //跳转id [可为课程id/计划id/文章id/ 是web页面默认为0] 
      "type":0, // 0 系统通知[默认] 1 好友新消息 2 新用户注册 3好友请求 4课程更新 
      "systype":1, //1 课程 2计划 3文章 4活动 5web页面跳转 
       "url":"http://www.imooc.com/abc.html" //为4web页面跳转使用 |非4web页面清除url字段 
     },
     "category"=>"CATEGORY_ID" //用来快捷处理消息唯一标示 
   }
 } 
  1. xcode项目配置推送
在application:  didFinishLaunchingWithOptions: 中书写

#pragma mark ----  注册远程通知的方法 ------

    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    }else {
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

代理方法

#pragma mark ----- 远程推送 -------
// 注册成功回调方法,其中deviceToken即为APNs返回的token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    NSString *token = [deviceToken description];
    token = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSLog(@"description %@", token);
    
}

// 注册失败回调方法,处理失败情况
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
    NSLog(@"注册失败,无法获取设备ID, 具体错误: %@", error);
    
}
// 获取到推送的数据
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSLog(@"userInfo == %@",userInfo);

    completionHandler(UIBackgroundFetchResultNewData);

    NSString * datas = [[userInfo objectForKey:@"aps"] objectForKey:@"datas"];
    
    NSDictionary * dict = [self dictionaryWithJsonString:datas];
    
    NSLog(@"dict == %@",dict);
}

/** 字符串类型Json转换字典 */
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    
    if(err) {
        NSLog(@"json解析失败:%@",err);
        return nil;
    }
    return dic;
}

4.参考资料
感谢一下读者的博客,本文只是对这些博文串起来叙述了APNS从证书落地到推送消息处理业务逻辑,有大量的参考资料代码,文字,感谢,统计标记在这儿,希望读者能去拜读一下前人栽的书。

配置流程没有问题,pushMe.php有问题
http://blog.csdn.net/showhilllee/article/details/8631734
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2

phpme.php推送用这个解决
http://www.cnblogs.com/hubj/archive/2012/06/14/2549816.html
http://www.intertech.com/Blog/push-notifications-tutorial-for-ios-9/http://blog.csdn.net/yujianxiang666/article/details/35260135

iOS9 Text Input
http://fancypixel.github.io/blog/2015/06/11/ios9-notifications-and-text-input/

WWDC2015APNS video
https://developer.apple.com/videos/play/wwdc2015-720/
http://54im.com/ios/手把手教你配置苹果apns推送服务.html#8.5_

你可能感兴趣的:(iOS - APNs源生推送)