dtvam48220 2015-04-27 19:41
浏览 42

Google Cloud Messaging推送通知仅适用一次(PHP,Android)

I am having trouble getting Google Cloud Messaging for Android working.

I have had it working before but it decides to stop working after the first couple of times.

In order to get it working again I have to delete my API key and recreate it.

I am using PHP with a SERVER API key with a IP whitelist of ::/0 (All IPv6 apparently)

Note: My android app requests a device message key each time the app is opened (Usually returns the same message key)

The Error I get is: Unauthorized Error 401

When I got to the following url to check my app message id i get 'invalid token'.

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=APA91bHuQEWGsvlRUhlSztNpqLVOZQGZPiGFHjQw2plcF-z8t29zvNNgNoDiRe-CbY9Fb-XcPQAFqJvy4HBfWTrTPPpzcY3pd5vX38WGalOsZ5iDiJeglpafLTC7eFkN4UA9JPKWZ4lqNiGLoH3w8W_GpFAFW5F-kLLzcbrPxwSFqyfUpmM8-14

The PHP code I am using is:

$data = array( 'message' => 'Hello World!222!' );

$ids = array('APA91bHuQEWGsvlRUhlSztNpqLVOZQGZPiGFHjQw2plcF-z8t29zvNNgNoDiRe-CbY9Fb-XcPQAFqJvy4HBfWTrTPPpzcY3pd5vX38WGalOsZ5iDiJeglpafLTC7eFkN4UA9JPKWZ4lqNiGLoH3w8W_GpFAFW5F-kLLzcbrPxwSFqyfUpmM8-14');

$apiKey = 'AIzaSyATkp_UTZh....'; //obviously the complete key is used...

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

$post = array('registration_ids'  => $ids, 'data' => $data);

$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;

Thanks for any help given.

EDIT: It seems to work since I unregistered my device and reregistered it with GCM. I am not sure if this is a permanent fix but it works for now.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度