【问题标题】:how to do push notification from android from php如何从 php 从 android 推送通知
【发布时间】:2013-12-20 18:54:58
【问题描述】:

嗨,我正在尝试将通知从我的 php Web 服务器发送到 android 手机...但我得到的响应为

Unauthorized Error 401

下面是我的代码

<?php $url = 'https://android.googleapis.com/gcm/send';
$device_ids = array('devise ID' );
$headers = array('Authorization: key=api key',
'Content-Type: application/json');
$t_data = array();
$t_data['message'] = array('Someone commented on your business.');
$t_json = array( 'registration_ids' => $device_ids , 'data' => $t_data );

$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $t_json ) );
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
if ($result === FALSE)
{
 die('Curl failed: ' . curl_error($ch));
}

curl_close($ch);

echo $result;
?>

我该如何解决这个错误

【问题讨论】:

  • 仔细检查API key
  • 是的,我检查过了,但没有运气
  • 你定义了 api key .. "Authorization: key=api key"。此 api 密钥是在应用程序在 Google 上注册 GCM 服务时发出的。检查这个。

标签: php android google-cloud-messaging


【解决方案1】:

这是我在自己的 GCM 项目中实现的工作脚本。尝试运行它。 确保对“api_key”和“registrationids”进行更改。

 <?php
        $api_key = "AIzBpx4XbXkhkjwKd8P-v2d1Jk";
        $registrationIDs = array("APA91bjkj3Rjlo8T_URx15NqvxY2mRyQ");
        $message = "congratulations";
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
                    'registration_ids'  => $registrationIDs,
                    'data'              => array( "message" => $message ),
                    );

        $headers = array(
                        'Authorization: key=' . $api_key,
                        '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( $fields ) );
        $result = curl_exec($ch);
        curl_close($ch);

    echo $result;
    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2015-11-13
    • 2022-09-28
    相关资源
    最近更新 更多