【问题标题】:How to set up the New Google Cloud Messaging API server implementation and Hosting using PHP?如何使用 PHP 设置新​​的 Google Cloud Messaging API 服务器实施和托管?
【发布时间】:2014-01-06 04:04:30
【问题描述】:

我正在使用新的 Google Cloud 消息传递功能,它已在客户端成功开发并接收推送通知而没有任何丢弃。但我在服务器上使用旧的发送功能。现在我想使用 PHP 实现新的发送函数(XMPP)。

我也在这里注册了https://services.google.com/fb/forms/gcm/,并从谷歌获得了回复邮件和密钥。

从中我得到我必须实现 SmackCcsClient Java 类和两个库。但我不知道如何将该文件托管到我的 PHP 服务器。

经过一番研究,我得到了 PHP 的函数和 PHP 的 xmphp 库

$conn = new XMPPHP_XMPP($host, $port, $user, $password, $resource, $server, $printlog, $loglevel);

但无法获得它所说的无法连接的成功。

【问题讨论】:

标签: php android google-cloud-messaging whatsapp xmpphp


【解决方案1】:

你可以使用这个类:

class gcm {


    public static function send_notification($deviceid, $message) {


        // include config


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

        $fields = array(
            'registration_ids' => (is_array($deviceid) ? $deviceid : array($deviceid)),
            'data' => array("message" =>$message),
        );

        $headers = array(
            'Authorization: key=YOUR-AUTH-KEY',
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

    }

}

【讨论】:

  • 谢谢,但是这种方法很旧,我们现在已经使用了一天,谷歌为我们提供了端口 5233 和 URL 的新方法gcm.googleapis.com。谷歌也提供了 java 方法,但我想在 php 中实现它。您还有其他参考资料吗?
  • 其实我也离开了这个方法,现在我正在使用 Amazon SNS。如果您必须为这两个平台(APNS 和 GCM)发布通知,它会更加灵活并且物有所值。在您的服务器端,您只触发一个通知,SNS 决定如何将通知发送到设备。看看吧。
  • 感谢指导,我去看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2014-03-02
  • 2019-03-31
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多