GCM - iOS推送通知在后台运行时无法正常工作

时间:2016-07-21 11:10:21

标签: php ios google-cloud-messaging

iOS GCM推送通知在后台运行时无效,但成功消息将在PHP中出现。我的代码:

$regId = "device token";
$message = "Test 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 = '';
    $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;
}

1 个答案:

答案 0 :(得分:0)

您是否在自己的应用中使用Phonegap / Cordova?我从昨天开始就在我的应用程序中有相同的行为,我认为这是因为GCM API中的一些更改。