android - 通过服务器端代码的 Firebase 通知

标签 android ios firebase firebase-cloud-messaging firebase-notifications

新的 Firebase 通知服务允许我们使用控制台 UI 向所有移动应用用户发布通知。

但我找不到任何用于 Firebase 通知服务的 REST API。我想根据事件通过服务器端代码自动向移动用户发送通知。 Firebase 通知服务是否有可以通过 HTTP/S 访问的 API?

最佳答案

是的,你可以。

1) 首先,获取您的 firebase 项目的服务器 key :

Project Settings -> Cloud Messaging Tab -> Copy the Server key.

2)现在,这是一个示例 php 脚本,用于向特定设备发送通知:

<?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) 执行输出:

{"multicast_id":8XXXD,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14XX"}]} 

关于android - 通过服务器端代码的 Firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37398898/

相关文章:

database - android SQLite 与平面文件

ios - 类型 "UIImageView"的值没有成员 'rating'

ios - 如何将电子邮件 ID 设置为 ABRecordRef 对象

ios - getTokenForcingRefresh 在 Firebase 中已弃用。用什么代替它?

android - 是否可以在 Firebase 中发送同步请求?

firebase - 了解 Firebase 的用户写入、全局读取规则

android - 更新 v7 支持 jar 导致类未找到异常

android - 异步任务在 2.3.3 中未运行 doInBackground

android - Fragment 类型中的方法 getLayoutInflater(Bundle) 不适用于参数 ()

ios - Apple mach-o 链接器错误