2016-09-30 267 views
1

我正在将PushKit集成到我的iOS应用程序中,并准备好客户端代码。我正在使用PKPushRegistry进行注册,并在我的代表中使用令牌接收PKPushCredential。我正在将此令牌注册到与APNS进行通信的服务器。接收PushKit VoIP通知iOS

我无法找到关于从我的服务器发送什么的好文档,以APNS发送VoIP推送通知给客户端。我只是发送一个正常的远程通知请求与可用内容:1推只是用我的VoIP .pem替换我的远程通知.pem

+0

它是否有效? – Hasya

回答

0

谢谢你提出这个问题。

请阅读下面关于Apple PushKit的一些有用信息,或者您也可以从苹果official page阅读。

PushKit框架为您的iOS应用程序提供接收远程服务器推送的类。推送可以是以下两种类型之一:标准和VoIP。标准推送可以像以前版本的iOS一样发送通知。 VoIP推送在标准推送之上提供了额外的功能,VoIP应用程序需要在向用户显示通知之前执行推送的按需处理。

1)Apple Push Kit不是一个简单的APNS,它是一个无声推送通知,因此当您收到推送通知时,您的应用程序不会作出反应。

2)当您收到Apple Push时,您必须安排UILocalNotification。你想 3)内容显示给用户展示在UILocalNotification

一些知名VOIP应用程序使用苹果推送套件的通知,如请告诉我应用程序,Skype和Facebook的Messenger的

以上所有应用程序Genrate UILocalNotification接收推后。

发送将用于生成本地通知的唯一有用信息。

+0

确实,Pushkit通知不会直接显示在通知中心。 @Dev你也必须安排本地通知,以便在通知中心收到通知。您还需要在客户端提供背景和VOIP许可。 – Hasya

+0

对不起,我们如何从developer.apple.com获得有关“Pushkit是无声通知”的信息?非常感谢。 – Yvonne

0

使用下面

<?php 

// Put your device token here (without spaces): 


     $deviceToken = '123456789'; 
// 


// Put your private key's passphrase here: 
$passphrase = 'ProjectName'; 

// Put your alert message here: 
$message = 'My first push notification!'; 



$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

// Open a connection to the APNS server 
$fp = stream_socket_client(
// 'ssl://gateway.push.apple.com:2195', $err, 
    'ssl://gateway.sandbox.push.apple.com:2195', $err, 
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

if (!$fp) 
    exit("Failed to connect: $err $errstr" . PHP_EOL); 

echo 'Connected to APNS' . PHP_EOL; 

// Create the payload body 

$body['aps'] = array(
        'content-available'=> 1, 
        'alert' => $message, 
        'sound' => 'default', 
        'badge' => 0, 
        ); 



// Encode the payload as JSON 

$payload = json_encode($body); 

// Build the binary notification 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 

// Send it to the server 
$result = fwrite($fp, $msg, strlen($msg)); 

if (!$result) 
    echo 'Message not delivered' . PHP_EOL; 
else 
    echo 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 

使用此simplepush.php文件命令创建PEM文件,并用它在上面的代码

$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem 

# Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert. 
$ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12 

Enter Import Password: 

MAC verified OK 

Enter PEM pass phrase: 

Verifying - Enter PEM pass phrase: 

# To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings. 
$ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem 

Enter pass phrase for PushChatKey1.pem: 

writing RSA key 

# To join the two .pem file into one file: 
$ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem 

之后去simplepush.php位置和消防指挥 - > php simplepush.php

这样你就可以测试你的推送工具包通知设置体系结构。

https://www.raywenderlich.com/123862/push-notifications-tutorial

Download

import UIKit 
import PushKit 


class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate{ 



func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 


    let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound] 
    application.registerForRemoteNotificationTypes(types) 

    self. PushKitRegistration() 

    return true 
} 



//MARK: - PushKitRegistration 

func PushKitRegistration() 
{ 

    let mainQueue = dispatch_get_main_queue() 
    // Create a push registry object 
    if #available(iOS 8.0, *) { 

     let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue) 

     // Set the registry's delegate to self 

     voipRegistry.delegate = self 

     // Set the push type to VoIP 

     voipRegistry.desiredPushTypes = [PKPushTypeVoIP] 

    } else { 
     // Fallback on earlier versions 
    } 


} 


@available(iOS 8.0, *) 
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) { 
    // Register VoIP push token (a property of PKPushCredentials) with server 

    let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes), 
     count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("") 

    print(hexString) 


} 


@available(iOS 8.0, *) 
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) { 
    // Process the received push 
    // From here you have to schedule your local notification 

} 

} 
+0

是否仍建议使用二进制消息stream_socket_client方法?我们不应该使用新的http/2系统从服务器发送推送通知吗? – Esaptonor