【问题标题】:Android Device is not receving notification when send data from PHP using REST api?使用 REST api 从 PHP 发送数据时,Android 设备未收到通知?
【发布时间】:2016-04-04 19:24:44
【问题描述】:

我收到了我的设备的 GCM 注册令牌,但是当我使用 PHP 的 REST api 发送消息时,它显示成功但未在设备上接收消息。 php代码

<?php 
 $data = array( 'message' => 'Hello World! First Message from PHP' );
 $ids = 'device registration token';
 // Send a GCM push
 sendGoogleCloudMessage(  $data, $ids );

function sendGoogleCloudMessage( $data, $ids )
{
// Insert real GCM API key from Google APIs Console
// https://code.google.com/apis/console/        
$apiKey = 'API Key'; //(server API Key)
// Define URL to GCM endpoint
$url = 'https://gcm-http.googleapis.com/gcm/send';
// Set GCM post variables (device IDs and push payload)     
$post = array(
                'to'  => $ids,
                'data'              => $data,
                );

// Set CURL request headers (authentication and type)       
$headers = array( 
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

// Initialize curl handle       
$ch = curl_init();

// Set URL to GCM endpoint      
curl_setopt( $ch, CURLOPT_URL, $url );

// Set request method to POST       
curl_setopt( $ch, CURLOPT_POST, true );

// Set our custom headers       
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );

// Get the response back as string instead of printing it       
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// Set JSON post data
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );

// Actually send the push   
$result = curl_exec( $ch );

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

// Close curl handle
curl_close( $ch );

// Debug GCM response       
echo $result;
print_r($result);
}
?>

在提交 PHP 文件时,它返回如下消息,似乎消息显示“成功”但设备没有收到消息。有人可以建议我是否需要采取额外的步骤或做错什么?

{"multicast_id":8550022549195779350,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1459795352923689%21f96bfff9fd7ecd"}]}

【问题讨论】:

    标签: php android google-cloud-messaging


    【解决方案1】:

    确保您的 API 密钥正确,并且您的服务器 IP 被证书列为受信任的。有时,连接 WiFi 的设备会在一段时间后对 GCM 无响应。尝试重新启动您的设备。它应该开始接收您发送的所有消息。

    另外,从这个SO question,检查你是否错误地使用了android:permission="com.google.android.gcm.c2dm.permission.SEND"而不是android:permission="com.google.android.c2dm.permission.SEND"

    希望这会有所帮助!

    【讨论】:

    • 感谢@abielita,权限正确。解析消息的问题。所以没问题,应用正在接收消息。
    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2015-02-09
    • 2017-07-04
    • 2016-01-03
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多