繁体   English   中英

使用文件PHP发送Firebase通知

[英]Send Firebase notification with file PHP

我的文件如下:

<?php

ignore_user_abort();
ob_start();

 $url = 'https://fcm.googleapis.com/fcm/send';

$Token = 'c128i0tYn..BA';

$fields = array('to' => $Token ,
'notification' => array('body' => 'HI', 'title' => ':)'));


define('GOOGLE_API_KEY', 'AIzaSyA9..4xU');

$headers = array(
      'Authorization:key='.GOOGLE_API_KEY,
      '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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
?>

它可以正常工作,但仅适用于将通知发送到特定设备(通过变量$ Token)。

现在,我想向所有安装了我的应用程序的设备发送通知,该怎么办? 谢谢!!

更改:

'to' => $Token , 

对于:

'registration_ids' => $ArrayOfTokens ,

参考https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream

您始终可以向主题发送通知,只是使您的通知格式如下:

{
   "to":"/topic/whatever-topic-you-want",
   "notification": {
                  "body":"I am the body",
                  "title":"I am title"
                   },
   "priority":"high"
}

在客户端,如果是android设备,则只需使用以下命令订阅主题“ whatever-topic-you-want”:

FirebaseMessaging.getInstance().subscribeToTopic("whatever-topic-you-want");

并且您订阅该主题的所有设备都将收到通知

参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM