2017-04-23 64 views
0

我的文件如下:发送火力地堡通知与文件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; 
?> 

的IT工作,完美,但只为将通知发送到特定设备(由变量$令牌)。

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

+1

一个典型的解决方案是让您的[应用订阅特定主题](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging)(例如'/ topics/messages'),然后[发送消息到该主题](https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_topics)。 –

回答

0

您可以随时发送通知到主题只让你通知格式如:

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

and at the c lient端,如果它的Android设备则只需订阅使用主题“任何话题,你想学”:

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

和您的所有设备订阅该主题将获得通知

Reference