2017-04-23 63 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