PushKit是一種新的消息通知方式徘跪,旨在幫助voip應(yīng)用(iOS 8)和watch OSComplication(iOS 9)減少電池的使用榄攀,提供更好的用戶體驗(yàn),app可以通過PushKit進(jìn)行喚醒,進(jìn)行一些操作脆贵,而不被用戶感知噪矛。
PushKit和APNS的區(qū)別
- APNS 遠(yuǎn)程推送 量蕊,在每次啟動(dòng)時(shí)只能獲取到一條推送內(nèi)容,并且相應(yīng)的處理必須在用戶進(jìn)行一定的操作后才能執(zhí)行艇挨。而PushKit可以在無操作的情況就能進(jìn)行消息的處理残炮。
- APNS需要獲取用戶的權(quán)限,如果用戶不同意接收推送缩滨,那么app就獲取不到deviceToken势就,相應(yīng)我們也就沒辦法推送給客戶端消息,而PushKit則可以脉漏,當(dāng)然如果用戶拒絕接收通知欄等推送苞冯,你想在PushKit的代理方法中,進(jìn)行本地推送侧巨,也是不可以的舅锄。
- PushKit類似于APNS的靜默推送,但是在送達(dá)率上比靜默推送可靠的多刃泡,用戶是可以沒有任何感知的巧娱。
PushKit的使用
申請(qǐng)證書
和申請(qǐng)其他證書一樣,但是voip證書并不區(qū)分生產(chǎn)和測試烘贴,只有一個(gè)
下載證書 voip_services.cer
app配置
打開Push Notifications開關(guān) 和 Background Modes里的Voice over IP開關(guān)
代碼設(shè)置
在AppDelegate中禁添,導(dǎo)入#import <PushKit/PushKit.h>
?? 這是一個(gè)iOS 8 以上的庫
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中進(jìn)行初始化設(shè)置。
PKPushRegistry * voipRegistry = [[PKPushRegistry alloc]initWithQueue:dispatch_get_main_queue()];
voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
接收Token的方法桨踪,這個(gè)Token是區(qū)別于APNS的Token的老翘,兩個(gè)不是一回事。并且這個(gè)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進(jìn)行推送,要注意的是卫键,一些第三方平臺(tái)是不支持PushKit的傀履。
Java需要p12的證書,php需要pem莉炉,這里需要將證書和專用密鑰導(dǎo)出(.p12)钓账,轉(zhuǎn)換成.pem 然后再將兩個(gè).pem合并起來
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';
//消息內(nèi)容
$message = 'A test message!';
//數(shù)字
$badge = 4;
$sound = 'default';
$body = array();
$body['aps'] = array('alert' => $message);
//把數(shù)組數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)
$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 到當(dāng)前目錄 執(zhí)行php -f php文件名.php
出現(xiàn)這個(gè)則表示成功了
再來看看Xcode
如果你的應(yīng)用想要使用PushKit,那么最好在提交AppStore時(shí)拍攝一個(gè)視頻來證明你的App是有用到Voip的(拍攝一下你們App通話時(shí)進(jìn)入后臺(tái)狀態(tài)欄的顯示一般就可以了)絮宁,否則被拒的可能性很大
我嘗試提交了一下梆暮,被拒原因如下:
2017年4月20日 上午1:37
發(fā)件人 Apple
- 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.
大家有想法一起交流,共同學(xué)習(xí)绍昂。