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

Joh*_*ski 5 php notifications ios

我正在努力通过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);    
}
Run Code Online (Sandbox Code Playgroud)

这是服务器上的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;
}
?>
Run Code Online (Sandbox Code Playgroud)

我已经尝试通过内部"数据"数组和外部"post"数组发送内容可用标志,我通过将其添加到两者来指示.

fetchCompletionHandler函数不接收该消息,而是等待应用程序再次处于活动状态并由正常应用程序接收:didRecieveRemoteNotification函数.可能是因为没有通过后台功能收到通知的原因?

Ole*_*kov 2

您应该向 GCM 提供content_available,而不是content-available(如 APN 中),并且您将能够在后台接收推送通知。

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

PS - 不到五分钟前我遇到了完全相同的问题。我们应该更仔细地阅读文档......