簡體   English   中英

使用PHP發送器和Swift在后台沒有收到IOS GCM推送通知

[英]IOS GCM push notifications not received in background using PHP sender and Swift

我正在努力通過GCM獲取有關IOS的后台通知 - 非后台通知已經正常工作。 以下是我整合背景通知的步驟:

  1. 在UIBackgroundmodes中啟用remote-notifications標記
  2. 將內容可用密鑰添加到我的通知負載。
  3. 編寫應用程序:didRecieveRemoteNotification:fetchCompletionHandler:在我的委托中。

以下是委托函數的代碼:

func application( application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
        println("Notification received2: \(userInfo)")

        GCMService.sharedInstance().appDidReceiveMessage(userInfo);

        NSNotificationCenter.defaultCenter().postNotificationName(messageKey, object: nil,
            userInfo: userInfo)
        handler(UIBackgroundFetchResult.NoData);    
}

這是服務器上的PHP腳本的代碼:

<?php
$regId = $_GET["regId"];
$message = $_GET["message"];

$data = array( 'price' => $message, 'sound' => 'default', 'body' => 'helloworld', 'title' => 'default', 'badge' => 12, 'content-available' => 1);

$ids = array( $regId);

sendGoogleCloudMessage(  $data, $ids );

function sendGoogleCloudMessage( $data, $ids )
{

    $apiKey = THE-API-KEY-THAT-I-AM-USING;

    $url = 'https://android.googleapis.com/gcm/send';

    $post = array(
                'registration_ids'  => $ids,
                'data'              => $data,
                'content-available' => 1,
    );

    $headers = array(
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_URL, $url );

    curl_setopt( $ch, CURLOPT_POST, true );

    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );

    $result = curl_exec( $ch );

    if ( curl_errno( $ch ) )
    {
        echo 'GCM error: ' . curl_error( $ch );
    }

    curl_close( $ch );

    echo $result;
}
?>

我已經嘗試通過內部“數據”數組和外部“post”數組發送內容可用標志,我通過將其添加到兩者來指示。

fetchCompletionHandler函數不接收該消息,而是等待應用程序再次處於活動狀態並由正常應用程序接收:didRecieveRemoteNotification函數。 可能是因為沒有通過后台功能收到通知的原因?

您應該向GCM提供content_available ,而不是內容可用 (如在APN中),您將能夠在后台接收推送通知。

https://developers.google.com/cloud-messaging/server-ref#send-downstream

PS - 不到五分鍾前,我遇到了同樣的問題。 我們應該更仔細地閱讀文檔......

暫無
暫無

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

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