如何从php向安卓发送通知?

19

我使用PHP和MySQL开发了一个网站。我希望在我网站上发布每篇文章时能够向安卓设备发送通知。

是否可以使用GCM服务器发送通知?如果可以,那么如何向所有已安装安卓应用的设备发送通知?


使用Laravel和Firebase FCM向Android和iOS客户端应用程序发送通知的API。 - John Lobo
3个回答

23
我决定发布我自己问题的答案。 现在Google推出了FCM。
<?php
define('API_ACCESS_KEY','Api key from Fcm add here');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='235zgagasd634sdgds46436';

    $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);


        echo $result;

3
我会尽力进行翻译,请问需要翻译的内容是:https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ - Mani kandan
如何获取令牌值? - natsumiyu
@natsumiyu,来自 Android 设备。你需要在 Android 端实现 FCM。 - scott
有一个新的 FCM API 实现 v1,这个还能用吗?还是需要进行一些修订? - Ajowi

15
首先遵循实施 GCM 客户端,并在您的 Android 应用中实现 GCM 客户端。
对于 PHP 的 GCM 服务器,您可以按以下方式实现。 根据您的需求编辑代码,例如在发布帖子时编写它。 有关实施 GCM 服务器的更多信息,请参见实施 GCM 服务器
<?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);
?>

还有一篇适合初学者的Android推送通知的文章。


这个很好用,太棒了!但是,我想知道如果谷歌更改了这个URL:https://android.googleapis.com/gcm/send会发生什么?这有多大可能性?如果可能的话,是否有任何PHP库可以跟上这些变化,我不想在谷歌更改它时手动更改我的应用程序中的URL。 - doubleOrt
我该如何获取API密钥?在Google控制台中有太多了。 - Alberto Acuña

1

代码中进行了一些更改,注释掉了定义API密钥的行,并将其作为我的PHP代码粘贴/插入。它可以正常工作。 请注释掉这行:

define('API_ACCESS_KEY','Api key from Fcm add here');

注释或替换此代码行:

'Authorization: key=' . API_ACCESS_KEY

//define('API_ACCESS_KEY','Api key from Fcm add here');

$apiKey = 'Api key from Fcm add here';

$fcmUrl = 'https://fcm.googleapis.com/fcm/send';

$token='235zgagasd634sdgds46436';

 $notification = [
        'title' =>'title',
        'body' => 'body of message.',
        'icon' =>'myIcon', 
        'sound' => 'mySound'
    ];
    $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

    $fcmNotification = [
        //'registration_ids' => $tokenList, //multple token array
        'to'        => $token, //single token
        'notification' => $notification,
        'data' => $extraNotificationData
    ];

    $headers = [
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
    ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接