簡體   English   中英

通過通知從后台喚醒 iOS 應用

[英]Wake up an iOS app from background with a notification

我有一個帶有 Firebase Cloud Messaging 的 iOS 應用程序來管理我的推送通知。 它運行良好,但我想在收到推送通知時打開我的應用程序(在后台狀態下)。 有點像 WhatsApp,具有自定義調用 (WebRTC) 視圖。

有什么想法嗎?

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

有沒有其他方法可以做到這一點......比如Signal應用程序。 響鈴通知(推送?),然后用戶單擊以打開應用程序。

直到 FCM 不支持靜默推送通知(推送套件)。

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

您收到靜默推送通知有效負載,然后您必須安排本地通知,因為靜默推送通知不會進入通知中心。 直到您的本地通知聲音文件播放為止(最長 30 秒),您的應用程序將在后台或終止狀態下調用。 您可以在此處執行 require 活動,但不能執行 UI 活動。

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

使用以下結構來完成您的任務。

使用這個 simplepush.php 文件

<?php

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


      $deviceToken = '1234567890123456789';
//


// 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);

使用以下命令創建 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

下載

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

}

}

在此處輸入圖片說明

在此處輸入圖片說明

如果您設置 Socket 架構,則並行。 然后套接字用於廣播數據,例如哪些用戶在線/離線,您需要知道而無需調用 API 然后您可以使用套接字,服務器上的套接字架構將在線/離線用戶的數據發送到設備,而無需從設備發出請求。

如果我能在某處幫助你,請告訴我。

祝你編碼愉快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM