推送通知服务器端实现

时间:2017-02-20 05:54:43

标签: php android firebase google-cloud-messaging firebase-cloud-messaging

最近我在我的应用程序最新版本中集成了FCM,但我以前的应用程序版本使用的是GCM。关于我们是否需要分离写GCM和FCM的背景cron的任何想法?

我的早期版本MY App 4.0并使用了GCM和当前版本的My App 4.1并集成了FCM。我想为版本和用户发送推送通知。那么我们是否需要为GCM和FCM编写服务器端程序?关于这种整合的任何想法。

FCM服务器端API:https://fcm.googleapis.com/fcm/send GCM Server Side API:https://android.googleapis.com/gcm/send

我们可以通过FCM Server端程序发送通知吗?还是单独需要为GCM和FCM编写程序?。

PHP中的FCM示例代码

<?php

function sendFCM($mess,$id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
        'to' => $id,
        'notification' => array (
                "body" => $mess,
                "title" => "Title",
                "icon" => "myicon"
        )
);
$fields = json_encode ( $fields );
$headers = array (
        'Authorization: key=' . "AIzaSyA9vpL9OuX6moOYw-4n3YTSXpoSGQVGnyM",
        '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 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

$result = curl_exec ( $ch );
curl_close ( $ch );
}

?>

2 个答案:

答案 0 :(得分:2)

FCM仍然与GCM兼容,因为它是核心。因此,在发送通知时切换到FCM端点(https://fcm.googleapis.com/fcm/send)仍应适用于具有GCM的应用版本。无需编写单独的程序。

答案 1 :(得分:2)

我的项目中有代码,您可以使用Google的Firebase进行尝试: Firebase Tutorial

                $notification_send ="Message to be sent";

                $server_key = '****************************';//Authorization Key
                $client = new Client();
                $client->setApiKey($server_key);
                $client->injectGuzzleHttpClient(new \GuzzleHttp\Client());
                $message = new Message();
                $message->setPriority('high');
                $message->addRecipient(new Topic('test'));
                $message
                    ->setNotification(new Notification('Reislivsmessen', $notification_send ))
                    ->setData(['key' => 'value']);

                $response = $client->send($message);

你必须创建主题,在这里&#34;测试&#34;。

我希望它也适合你。

相关问题