PushKit是一種新的消息通知方式,旨在幫助voip應用(iOS 8)和watch OSComplication(iOS 9)減少電池的使用,提供更好的用戶體驗,app可以通過PushKit進行喚醒,進行一些操作,而不被用戶感知。
PushKit和APNS的區別
- APNS 遠程推送 ,在每次啟動時只能獲取到一條推送內容,并且相應的處理必須在用戶進行一定的操作后才能執行。而PushKit可以在無操作的情況就能進行消息的處理。
- APNS需要獲取用戶的權限,如果用戶不同意接收推送,那么app就獲取不到deviceToken,相應我們也就沒辦法推送給客戶端消息,而PushKit則可以,當然如果用戶拒絕接收通知欄等推送,你想在PushKit的代理方法中,進行本地推送,也是不可以的。
- PushKit類似于APNS的靜默推送,但是在送達率上比靜默推送可靠的多,用戶是可以沒有任何感知的。
PushKit的使用
申請證書
和申請其他證書一樣,但是voip證書并不區分生產和測試,只有一個
下載證書 voip_services.cer
app配置
打開Push Notifications開關 和 Background Modes里的Voice over IP開關
代碼設置
在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合并起來
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
出現這個則表示成功了
再來看看Xcode
如果你的應用想要使用PushKit,那么最好在提交AppStore時拍攝一個視頻來證明你的App是有用到Voip的(拍攝一下你們App通話時進入后臺狀態欄的顯示一般就可以了),否則被拒的可能性很大
我嘗試提交了一下,被拒原因如下:
2017年4月20日 上午1:37
發件人 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.
大家有想法一起交流,共同學習。