使用PHP实现GCM(Google Cloud Messaging)

218

http://www.sherif.mobi/2012/07/gcm-php-push-server.html - Sherif elKhatib
4
我已经编写了一个小型的面向对象编程库,并实现了GCM服务器。希望它能对某些人有所帮助 :) 在GitHub上查看 - https://github.com/CodeMonkeysRu/GCMMessage - iVariable
1
@HelmiB:我在网站上尝试了你的代码,它没有出现任何错误,但是 $result 是空的。而且消息也无法发送。请帮帮我,我真的很需要它。 - user2064667
我的 GCMMessage 分叉支持指数备份,这是使用 Google API 必需的。它使用 Redis 服务器排队消息,并支持新的端点和 iOS:https://github.com/stevetauber/php-gcm-queue - Steve Tauber
非常简单,您只需要一个应用服务器、GCM服务器和托管该服务的应用程序托管。请参考此示例。这里本地主机充当应用服务器 http://www.feelzdroid.com/2016/02/android-google-cloud-messaging-push-notifications-gcm-tutorial.html。 - Naruto
13个回答

0
使用这个
 function pnstest(){

                $data = array('post_id'=>'12345','title'=>'A Blog post', 'message' =>'test msg');

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

                $server_key = 'AIzaSyDVpDdS7EyNgMUpoZV6sI2p-cG';

                $target ='fO3JGJw4CXI:APA91bFKvHv8wzZ05w2JQSor6D8lFvEGE_jHZGDAKzFmKWc73LABnumtRosWuJx--I4SoyF1XQ4w01P77MKft33grAPhA8g-wuBPZTgmgttaC9U4S3uCHjdDn5c3YHAnBF3H';

                $fields = array();
                $fields['data'] = $data;
                if(is_array($target)){
                    $fields['registration_ids'] = $target;
                }else{
                    $fields['to'] = $target;
                }

                //header with content_type api key
                $headers = array(
                    'Content-Type:application/json',
                  'Authorization:key='.$server_key
                );

                $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_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $result = curl_exec($ch);
                if ($result === FALSE) {
                    die('FCM Send Error: ' . curl_error($ch));
                }
                curl_close($ch);
                return $result;

}

0
这是我从 CodeMonkeysRU fork 的一个库。
我 fork 的原因是 Google 要求使用指数退避算法。我使用 redis 服务器排队消息,并在一定时间后重新发送。
我还更新了它以支持 iOS。

https://github.com/stevetauber/php-gcm-queue


你好,能否请您指出iOS所做的更改?与原始库相比,我并没有看到什么特别之处。 - fralbo
@2ndGAB 我分叉的主要原因是指数退避。至于iOS的更改,你可以在这里阅读相关信息:https://developers.google.com/cloud-messaging/http-server-ref#notification-payload-support - Steve Tauber

0

我知道这是一个晚回答,但对于那些想要使用当前的 FCM 格式(GCM 已被弃用)开发类似应用程序的人可能会有用。

以下 PHP 代码已用于按主题发送播客。所有注册了所提到的频道/主题的应用程序都将收到此推送通知。

<?php

try{
$fcm_token = 'your fcm token';
$service_url = 'https://fcm.googleapis.com/fcm/send';
$channel = '/topics/'.$adminChannel;
echo $channel.'</br>';
      $curl_post_body = array('to' => $channel,
        'content_available' => true,
        'notification' => array('click_action' => 'action_open',
                            'body'=> $contentTitle,
                            'title'=>'Title '.$contentCurrentCat. ' Updates' ,
                            'message'=>'44'),
        'data'=> array('click_action' => 'action_open',
                            'body'=>'test',
                            'title'=>'test',
                            'message'=>$catTitleId));

        $headers = array(
        'Content-Type:application/json',
        'Authorization:key='.$fcm_token);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $service_url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curl_post_body));

    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('FCM Send Error: ' . curl_error($ch));
        echo 'failure';
    }else{

    echo 'success' .$result;
    }
    curl_close($ch);
    return $result;

}
catch(Exception $e){

    echo 'Message: ' .$e->getMessage();
}
?>

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