如何从网站聊天发送Android推送通知

时间:2015-04-15 17:33:50

标签: java php android jquery

我已经完成了像whatsapp设计的网站聊天编程,我已经在android中创建了应用程序。这个网站聊天的应用浏览器(只显示像Chrome浏览器的网站)。 我需要帮助才能了解如何在用户收到消息时获取应用程序的通知Android通知应显示“您有新消息”。

我的问题是我如何使用推送通知将数据从网站发送到Android

The website chat

2 个答案:

答案 0 :(得分:0)

您的Android应用程序应该是GCM注册的,然后您可以从您的网站发送GCM,然后将其推送到您的应用程序。

有关详细信息,请参阅:https://developer.android.com/google/gcm/client.html

有关详细教程,请参阅:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

答案 1 :(得分:0)

 public function notificationToAndroidAction($pushId){

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

        $fields = array(
            'registration_ids' => array($pushId),
            'data' => array("message" => 'hello!'),
        );

        $headers = array(
            'Authorization: key=' . GOOGLE_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);
        // Disabling SSL Certificate support temporarily
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

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

        curl_close($ch);

        return 'sent';
    }