推送通知服务器端实现

Pushnotification Server side Implementation

最近我在我的最新版本的应用程序中集成了 FCM,但我以前版本的应用程序使用的是 GCM。关于我们是否需要为 GCM 和 FCM 分离编写后台 cron 有什么想法吗?

我以前的版本我的应用程序 4.0 和使用 GCM 和当前版本我的应用程序 4.1 并集成了 FCM。我想为版本和用户发送推送通知。那么我们是否需要为 GCM 和 FCM 编写服务器端程序,对吗?关于此集成的任何想法。

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

我们是否可以通过 FCM 服务器端程序发送通知?还是需要分别为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 );
}

?>

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

我的项目中有工作代码,您可以使用 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);

您必须创建主题,这里是"test"。

希望对你也有用。