加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

pushKit学习

(2019-02-21 15:59:40)
分类: iOS

pushkit方式,有别于普通的apns,它不会弹出通知,而是悄悄的告诉我们的app有推送过来,让app做出相应的处理。

 所以当pushkit回调方法里使用本地推送来处理

当app在线,后台,离线pushkit都有推送此时当在线时不希望弹出本地推送的状态栏。



第一步:申请证书。

步骤:

1)跟apns差不多,在Creating a VoIP certificate in the Apple Developer Member Center

Production中选择VoIP Services certificate.

2)生成的证书CertificateSigningRequest.cerSigningRequest,下载证书voip_services.cer.

客户端和pushkit server端都安装这个证书

3)客户端从“钥匙串”->左侧:我的证书->VoIP Services:com.testxx.suiyizhengshu.右击导出此证书。格式可以选:.p12,.cer等格式。

4VoIP不区分生产环境还是开发环境(production or development)也就是生成的证书是一个。


第二步:ios客户端

1Xcode Project > Capabilities paneEnabling the VoIP background mode in an app

Background Modes:Voice over IP。


题外话:我因为个人原因很久没升级xcode,发现没有这选项,xcode升级到10就出现了。当然mac系统也升级到MacOSHighSierra10.16.6。话说升级mac,各种折腾,后来重装mac系统才升级成功。

借鉴:

a.https://jingyan.baidu.com/article/d169e1867d1d7d436711d86a.html 重装Mac系统,苹果电脑重装,其实很简单

b.https://support.apple.com/zh-cn/macos/high-sierra 升级到macOS High Sierra

c.http://blog.sina.com.cn/s/blog_67a6d62e0102xae7.html Mac电脑如何清理硬盘空间


2

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    //注册pushkit

    [self registePushKitApi];

}


//注册pushkit

- (void)registePushKitApi {

    // 注册PushKit

    PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue: nil];

    pushRegistry.delegate = self;

    pushRegistry.desiredPushTypes = [NSSet setWithObject: PKPushTypeVoIP];

    

    // 注册push权限,用于显示本地推送

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

}


#pragma - pushkitDelegate

// PushKit delete

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type {

    //去掉server发回来token中的<>,空格不去掉,具体情况视PushKit server决定

    NSString * tokenString = [[[credentials.token description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""];

    SpeLog(@"PushKit toke: %@", tokenString);

    

   

}

备注:把credentials.token提供给pushkit server端。


- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type {

    

    // payload中获取推送信息

    NSDictionary *dic = payload.dictionaryPayload;

    NSDictionary *apsDic = dic[@"aps"];

    NSString *msgId = dic[@"msg_id"];

    

    // 此处也可以根据message_id获取消息具体内容,然后在block中发送本地推送

    // ...

    

    // 生成本地推送

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];

    localNotification.fireDate = [NSDate date];

    localNotification.alertBody = apsDic[@"alert"];

    localNotification.soundName = @"default";

    localNotification.alertTitle = @"一条测试pushkit的推送";

    localNotification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys: msgId, @"msgid", nil];

    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];

}


备注:pushkit server端的payload即是上面回调函数中的payload.dictionaryPayload:

格式如下:

{

“aps":{"alert":"This is some fancy message.","badge”:1},

“msg_id”:”12345678”

}


-(void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(PKPushType)type {

    NSLog(@"didInvalidatePushTokenForType");

}



第三步骤,Pushkit server端解析

server端代码:https://github.com/stefanhafeneger/PushMeBaby 

1.打开:README.md。就是把生成的证书起个生产证书名称:aps0.cer,开发模式:aps_development0.cer.其实是同一个证书,只是名字不一样。替换项目中的./PushMeBaby-master这个目录下面的aps0.ceraps—development0.cer

2.server端production地址:gateway.push.apple.comdevelopment地址:gateway.sandbox.push.apple.com

.const char *hostName = _production?"gateway.push.apple.com":"gateway.sandbox.push.apple.com";

3.self->_certificateRef:

com.testxx.suiyizhengshu i: Apple Worldwide Developer Relations Certification Authority>



参考文章:

1.https://www.jianshu.com/p/213641cf524a PushKit 的简单实验 作者:谁动了我的芝麻糖

2.Pushkit官网:https://developer.apple.com/library/archive/documentation/Performance/Conceptual/EnergyGuide-iOS/OptimizeVoIP.html 

3.https://www.jianshu.com/p/afc8de658931 PushKit的使用 作者:Pooonyo 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有