【问题标题】:Pushnotification Server side ImplementationPushnotification 服务器端实现
【发布时间】:2017-07-09 07:24:21
【问题描述】:

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

My Previous version MY App 4.0 并使用 GCM 和 Current version My App 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 );
}

?>

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案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);
      

      你必须创建主题,这里是“测试”。

      我希望它也对你有用。

      【讨论】:

        猜你喜欢
        • 2018-05-19
        • 1970-01-01
        • 1970-01-01
        • 2013-07-21
        • 2013-04-11
        • 1970-01-01
        • 2019-03-22
        • 2020-02-08
        • 2019-01-26
        相关资源
        最近更新 更多