当前位置: 首页>编程语言>正文

iOS中不一样的推送---PushKit

PushKit是一种新的消息通知方式,旨在帮助voip应用(iOS 8)和watch OSComplication(iOS 9)减少电池的使用,提供更好的用户体验,app可以通过PushKit进行唤醒,进行一些操作,而不被用户感知。

PushKit和APNS的区别

  • APNS 远程推送 ,在每次启动时只能获取到一条推送内容,并且相应的处理必须在用户进行一定的操作后才能执行。而PushKit可以在无操作的情况就能进行消息的处理。
  • APNS需要获取用户的权限,如果用户不同意接收推送,那么app就获取不到deviceToken,相应我们也就没办法推送给客户端消息,而PushKit则可以,当然如果用户拒绝接收通知栏等推送,你想在PushKit的代理方法中,进行本地推送,也是不可以的。
  • PushKit类似于APNS的静默推送,但是在送达率上比静默推送可靠的多,用户是可以没有任何感知的。

PushKit的使用

申请证书

和申请其他证书一样,但是voip证书并不区分生产和测试,只有一个


iOS中不一样的推送---PushKit,第1张
证书申请

下载证书 voip_services.cer

app配置

打开Push Notifications开关 和 Background Modes里的Voice over IP开关

iOS中不一样的推送---PushKit,第2张
Push Notifications
iOS中不一样的推送---PushKit,第3张
background Modes

代码设置

在AppDelegate中,导入#import <PushKit/PushKit.h>
⚠️ 这是一个iOS 8 以上的库

在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中进行初始化设置。

PKPushRegistry * voipRegistry = [[PKPushRegistry alloc]initWithQueue:dispatch_get_main_queue()];
    
    voipRegistry.delegate = self;
    voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

接收Token的方法,这个Token是区别于APNS的Token的,两个不是一回事。并且这个Token无论App卸载重装,都是唯一的

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
    
    
    NSString *str = [NSString stringWithFormat:@"%@",credentials.token];
    
    NSString * tokenStr = [[[str stringByReplacingOccurrencesOfString:@"<" withString:@""]
                            stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"TOKEN =     %@",tokenStr);
}

接收消息的方法,打印一下

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

    NSDictionary *dic = [payload.dictionaryPayload objectForKey:@"aps"];
    NSLog(@"******************* push Kit %@***********************",dic);
}

测试

测试一下,这里用PHP进行推送,要注意的是,一些第三方平台是不支持PushKit的。

Java需要p12的证书,php需要pem,这里需要将证书和专用密钥导出(.p12),转换成.pem 然后再将两个.pem合并起来

iOS中不一样的推送---PushKit,第4张
钥匙串
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
openssl pkcs12 -nocerts -out key.pem -in key.p12
cat cert.pem key.pem > ck.pem

PHP代码

<?php
$deviceToken = '*******************';
//ck.pem密码
$pass = '123456';   
//消息内容
$message = 'A test message!';
//数字
$badge = 4;
$sound = 'default';
$body = array();
$body['aps'] = array('alert' => $message);
//把数组数据转换为json数据
$payload = json_encode($body);
echo strlen($payload),"\r\n";
 $ctx = stream_context_create([
            'ssl' => [
                'verify_peer'      => false,
                'verify_peer_name' => false
                // 'cafile'           => '/path/to/bundle/entrust_2048_ca.cer',
            ]
        ]);
// $pem = dirname(__FILE__) .'/'.'ck.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');  
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('tls://gateway.sandbox.push.apple.com:2195',$err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
    print "Failed to connect $err $errstr\n";
    return;
}
else {
   print "Connection OK\n<br/>";
}
// send message
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "\n";  
fwrite($fp, $msg);
fclose($fp);
/*
35 Connection OK 
Sending message :{"aps":{"alert":"A test message!"}} �
*/

cd 到当前目录 执行php -f php文件名.php

出现这个则表示成功了


iOS中不一样的推送---PushKit,第5张
终端

再来看看Xcode


iOS中不一样的推送---PushKit,第6张
Xcode

如果你的应用想要使用PushKit,那么最好在提交AppStore时拍摄一个视频来证明你的App是有用到Voip的(拍摄一下你们App通话时进入后台状态栏的显示一般就可以了),否则被拒的可能性很大
我尝试提交了一下,被拒原因如下:

2017年4月20日 上午1:37
发件人 Apple

  1. 1 Performance: App Completeness
    Guideline 2.1 - Information Needed

We have started the review of your app, but we are not able to continue because we need access to a video that demonstrates your app in use on an iOS device. Specifically, please show voip functionality of your app.

Next Steps

To help us proceed with the review of your app, please provide us with a link to a demo video in the App Review Information section of iTunes Connect and reply to this message in Resolution Center.

To provide a link to a demo video:

  • Log in to iTunes Connect
  • Click on "My Apps"
  • Select your app
  • Click on the app version on the left side of the screen
  • Scroll down to "App Review Information"
  • Provide demo video access details in the "Notes" section
  • Click "Save"
  • Once you've completed all changes, click the "Submit for Review" button at the top of the App Version Information page.

Once this information is available, we can continue with the review of your app.

大家有想法一起交流,共同学习。


https://www.xamrdz.com/lan/5s82016660.html

相关文章: