首页 文章

推送通知服务器端实现

提问于
浏览
2

最近我在我的应用程序最新版本中集成了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服务器端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 回答

  • 2

    FCM仍然与GCM兼容,因为它是核心 . 因此,在发送通知时切换到FCM endpoints (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);
    

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

    我希望它对你也有用 .

相关问题