美文网首页
iOS APNS 推送

iOS APNS 推送

作者: codeing小牛 | 来源:发表于2017-01-18 17:06 被阅读36次

    因为业务需求考虑使用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);
    }
    
    

    相关文章

      网友评论

          本文标题:iOS APNS 推送

          本文链接:https://www.haomeiwen.com/subject/stcbbttx.html