iOS APNS 推送

因为业务需求考虑使用APNs 实现远程推送
(1) 程序内注册通知将token 发送给服务器(重点在token的处理)
(2)导出p12 文件提供给服务器

// 测试证书

屏幕快照 2017-01-18 下午4.56.03.png

// 生产证书

屏幕快照 2017-01-18 下午4.55.49.png

服务器端使用c#实现:代码参考 https://github.com/Redth/PushSharp/

注册远程通知
-(void)registForRemoteNotification{

    UIUserNotificationType types = (UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert);
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}

实现代理方法

// 注册成功
//(刚开始对对token 的处理还有点小纠结 因为注册成功后返回的类型为NSData类型的数据 而服务器端需要的是字符串 ,不知道该对Token 做什么处理才会被苹果远程推送服务器识别 以下处理方法亲测有效)
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
   
    NSString *token =
    [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"
                                                           withString:@""]
      stringByReplacingOccurrencesOfString:@">"
      withString:@""]
     stringByReplacingOccurrencesOfString:@" "
     withString:@""];

// 将token 发送给服务器即可    
}

// 注册失败回调方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容