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

PushKit是一種新的消息通知方式,旨在幫助voip應(yīng)用(iOS 8)和watch OSComplication(iOS 9)減少電池的使用,提供更好的用戶體驗(yàn),app可以通過PushKit進(jìn)行喚醒,進(jìn)行一些操作,而不被用戶感知。

PushKit和APNS的區(qū)別

  • APNS 遠(yuǎn)程推送 ,在每次啟動時只能獲取到一條推送內(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的使用

申請證書

和申請其他證書一樣,但是voip證書并不區(qū)分生產(chǎn)和測試,只有一個


證書申請

下載證書 voip_services.cer

app配置

打開Push Notifications開關(guān) 和 Background Modes里的Voice over IP開關(guān)

Push Notifications
background Modes

代碼設(shè)置

在AppDelegate中,導(dǎo)入#import <PushKit/PushKit.h>
?? 這是一個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的方法,這個Token是區(qū)別于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進(jìn)行推送,要注意的是,一些第三方平臺是不支持PushKit的。

Java需要p12的證書,php需要pem,這里需要將證書和專用密鑰導(dǎo)出(.p12),轉(zhuǎn)換成.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';   
//消息內(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)這個則表示成功了


終端

再來看看Xcode


Xcode

如果你的應(yīng)用想要使用PushKit,那么最好在提交AppStore時拍攝一個視頻來證明你的App是有用到Voip的(拍攝一下你們App通話時進(jìn)入后臺狀態(tài)欄的顯示一般就可以了),否則被拒的可能性很大
我嘗試提交了一下,被拒原因如下:

2017年4月20日 上午1:37
發(fā)件人 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.

大家有想法一起交流,共同學(xué)習(xí)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容