如何从PHP发送通知到Android?通知、PHP、Android

2023-09-05 04:20:02 作者:爱,我会放在心里面。

我已经开发使用PHP的网站,mysql.I要发送通知到Android装置,该装置上传到我的网站,每一个岗位。

I have developed a website using php and mysql.I want to send notification to Android device for every post which is uploaded to my website.

是否有可能使用GCM服务器发送通知?如果是的话,那我怎么才能发送通知,该通知已经安装了Android应用程序的所有设备?

Is it possible to send notification using GCM Server? If yes, then how can I send notification to all devices which have the Android app installed?

推荐答案

首先按照此https://developers.google.com/cloud-messaging/android/client并实施GCM客户端在你的Andr​​oid应用程序。

First of all follow this https://developers.google.com/cloud-messaging/android/client and implement the gcm client in your android app.

有关在PHP GCM服务器,您可以通过以下方式实施。 根据您的需要,比如$ C C是$发布帖子的时候为编辑你的code。 欲了解更多有关实现GCM服务器请点击此链接https://developers.google.com/cloud-messaging/server

For GCM Server in php you can implement it in the following way. Edit your code according to your need like code it for when publishing the post. For more about implementing the GCM Server follow this link https://developers.google.com/cloud-messaging/server

<?php
    // Replace with the real server API key from Google APIs
    $apiKey = "your api key";

    // Replace with the real client registration IDs
    $registrationIDs = array( "reg id1","reg id2");

    // Message to be sent
    $message = "Your message e.g. the title of post";

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

    $fields = array(
        'registration_ids' => $registrationIDs,
        'data' => array( "message" => $message ),
    );
    $headers = array(
        'Authorization: key=' . $apiKey,
        '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);
    //curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields));

    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( $fields));

    // Execute post
    $result = curl_exec($ch);

    // Close connection
    curl_close($ch);
    // print the result if you really need to print else neglate thi
    echo $result;
    //print_r($result);
    //var_dump($result);
?>

还有一个好帖子http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/适合初学者。