通过服务器端代码的Firebase通知 [英] Firebase Notifications through server-side code
问题描述
但是我找不到Firebase通知服务的任何REST API。我想通过基于事件的服务器端代码自动发送通知给移动用户。 Firebase Notifications Service是否具有可通过HTTP / S访问的API?是的,您可以。
1)首先,获取您的firebase项目的服务器密钥:
项目设置 - >云消息传递标签 - >复制服务器密钥。
2)现在,这里是一个示例php脚本发送通知一个特定的设备:
<?php
$ ch = curl_init(https://fcm.googleapis。 COM / FCM /发送);
//设备令牌。
$ token =device_token_here;
//通知的标题。
$ title =北方记忆;
//通知的正文。
$ body =熊岛不认识国王,而是北方的国王,他的名字是鲜明的。
//创建通知数组。
$ notification = array('title'=> $ title,'body'=> $ body);
//这个数组包含令牌和通知。 'to'属性存储令牌。
$ arrayToSend = array('to'=> $ token,'notification'=> $ notification);
//从上面的数组中生成JSON编码的字符串。
$ json = json_encode($ arrayToSend);
//安装标题:
$ headers = array();
$ headers [] ='Content-Type:application / json';
$ headers [] ='Authorization:key = your_server_key_here';
//设置卷曲,添加标题和发布参数。
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,POST);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ json);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
//发送请求
curl_exec($ ch);
//关闭请求
curl_close($ ch);
?>
3)执行输出:
{multicast_id:8XXXD,success:1,failure:0,canonical_ids:0,results:[{message_id :0:14XX}]}
The new Firebase notifications service allows us to use a Console UI to post notifications to all mobile app users.
But I could not find any REST API for the Firebase Notifications Service. I want to send notifications to mobile users automatically through server-side code based on an event. Does Firebase Notifications Service have an API that can be accessed over HTTP/S?
Yes,you can.
1) Firstly, get the server key of your firebase project:
Project Settings -> Cloud Messaging Tab -> Copy the Server key.
2) Now, here is a sample php script to send notification to a specific device:
<?php
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
//The device token.
$token = "device_token_here";
//Title of the Notification.
$title = "The North Remembers";
//Body of the Notification.
$body = "Bear island knows no king but the king in the north, whose name is stark.";
//Creating the notification array.
$notification = array('title' =>$title , 'body' => $body);
//This array contains, the token and the notification. The 'to' attribute stores the token.
$arrayToSend = array('to' => $token, 'notification' => $notification);
//Generating JSON encoded string form the above array.
$json = json_encode($arrayToSend);
//Setup headers:
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key= your_server_key_here';
//Setup curl, add headers and post parameters.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
curl_exec($ch);
//Close request
curl_close($ch);
?>
3) Output on execution:
{"multicast_id":8XXXD,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14XX"}]}
这篇关于通过服务器端代码的Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!