我只需要向少数设备发送推送通知。我把他们的设备令牌放在一个数组中。我是否需要为此创建主题或组?
我可以使用for循环单独发送推送通知,但是有更好的方法吗?
编辑:请不要在不理解的情况下将此问题标记为重复。问题
FCM (Firebase Cloud Messaging) Send to multiple devices
有使用FCM令牌和组的答案,我已经明确表示这不是我的要求。就其他问题提到的'registration_ids'而言,官方文档中没有提及它,如果提到的话,请将其指出为有效答案。
答案 0 :(得分:3)
使用“registration_ids”代替“to”。
var message = {
registration_ids:['id1','id2','id3'],
notification: {
title: 'Hello There...!',
body: 'this is test notification'
}
};
希望它有效.. !!!
答案 1 :(得分:0)
我知道这已被回答,而不是最新的问题。
当然,您可以构建string array
并按照Google文档find here中提到的方式发送它。
HTTP POST请求
例如,要将具有注册ID 51的设备添加到appUser-Chris,您将发送此请求:
{
"operation": "add",
"notification_key_name": "appUser-Chris",
"notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ",
"registration_ids": ["51"] //you can insert something more of your strings here.
}
响应格式
添加或删除设备的成功请求会返回如下所示的notification_key:
{
"notification_key": "APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ"
}
答案 2 :(得分:0)
<?php
// Your code here!
class FCM
{
private $key;
function __construct($api_key)
{
$this->key = $api_key;
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $notification)
{
$fields = array ( 'to' => $registatoin_ids,'notification' => $notification); //'restricted_package_name' => "com.apps.firebasenotificationphp");
$headers = array (
'Authorization: key=' . $this->key,
'Content-Type: application/json'
);
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send" );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );
return curl_exec ( $ch );
curl_close ( $ch );
}
}
$api_key = "";
$device_id = "";
$fcm = new FCM($api_key);
$arrNotification = array ("body" => "hii","title" => "hello",'image' => "https://www.gstatic.com/devrel-devsite/prod/vb1c70bbe2f68b543db3deb1075af42e62f8f21e5fc703b8398dc6b9860f1711f/firebase/images/lockup.png",'link' => "",'sound' => "default");
echo $fcm->send_notification($device_id,$arrNotification);
?>