2016-10-06 205 views
1

我有一個iOS應用程序,使用Firebase雲消息傳遞來管理我的推送通知。它運行良好,但是當我收到我的推送通知時,我想打開我的應用程序(處於後臺狀態)。有點像WhatsApp,具有自定義調用(WebRTC)視圖。從後臺打開iOS應用程序

任何想法?謝謝。

編輯:

顯然我應該使用PushKit做我想做的事情。

是否有其他方式可以做到這一點...就像Signal應用程序。響鈴通知(推?),然後用戶點擊打開應用程序。

+0

您需要實現Apple PushKit而不是APNS。 – iMHitesh

+0

謝謝。我怎麼能實現PushKit? – parnissolle

+0

https://developer.apple.com/reference/pushkit – iMHitesh

回答

1

直到FCM不支持靜默推送通知(推送工具包)。

使用Pushkit是目前僅向設備發送靜默推送通知的技術。

您收到靜默推送通知有效負載的一個,那麼您必須安排本地通知,因爲靜默推送通知不會進入通知中心。直到您的本地通知聲音文件播放(最多30秒),您的應用程序將在後臺調用或終止狀態。你可以在這裏做需要的活動,但不需要UI活動。

在點擊交互式本地通知時,您可以進一步進行UI活動。

使用下面的結構來實現您的任務。

使用下面

<?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 

} 

} 

enter image description here

enter image description here

與此同時,如果你設置的Socket架構。然後Socket用於廣播數據,例如哪些用戶在線/離線,並且您不需要調用API就可以知道,然後您可以使用套接字,服務器上的套接字架構將在線/離線用戶的數據發送到設備,而無需請求設備。

讓我知道,如果我可以幫助你的地方。

祝您編程愉快。

+0

嘿,謝謝。所以我必須實現PushKit而不是推送通知。事實上,這正是我想要的應用程序...但我沒有任何想法如何實現。我已經閱讀了Apple PushKit的參考資料,但我有點迷糊。你可以幫幫我嗎 ?謝謝。 – parnissolle

+0

感謝您的回答。我會很快嘗試你的代碼,並在需要時回覆給你。只是另一個問題......我必須創建一個Apple開發人員證書來執行此操作嗎? – parnissolle

+0

歡迎您隨時與我聯繫,進行VOIP相關諮詢。 – Hasya

相關問題