2016-09-10 163 views
2

我想創建應用程序的通知。通知必須從php文件發送到firebase然後發送到設備。起初,我試圖用firebase控制檯發送,它的工作原理非常完美。但是,當我嘗試使用PHP發送notificaion然後我有問題。它說成功發佈了,但我沒有收到任何通知。還有就是我發送通知PHP:ios發送與firebase通知

<?php 
function send_notification ($tokens, $message) 
{ 

    $url = 'https://fcm.googleapis.com/fcm/send'; 
    $fields = array(
     'to' => $tokens, 
     'data' => $message 
     ); 
    $headers = array(
     'Authorization:key = my 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_RETURNTRANSFER, true); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    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($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

$data = array("alert" => "How it is going today"); 
$token = "fJVM7YrWGQg......"; 


$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE"); 
$message_status = send_notification($token, $message); 
echo $message_status; 

>

...我已經尋找解決它。但沒有任何幫助。

回答

2

Hej大家,我找到了解決辦法。如果有人搜索希望它會有所幫助:

// API access key from Google API's Console 
define('API_ACCESS_KEY', 'YOUR_FIREBASE_API_KEY'); 
$registrationIds = array("devices firebasetoken here."); 
// prep the bundle 
$msg = array(
     'body' => "message text", 
     'title'  => "message title", 
     'vibrate' => 1, 
     'sound'  => 1, 
    ); 
$fields = array(
      'registration_ids' => $registrationIds, 
      'notification'  => $msg 
     ); 

$headers = array(
      'Authorization: key=' . API_ACCESS_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_SSL_VERIFYPEER, false); 
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
echo $result; 
+0

嗨@MRustamzade可以告訴我,請你如何處理它內的應用程序。謝謝,請讓我知道。 –

+0

@AvinashMishra我也是這樣做的:https://firebase.google.com/docs/cloud-messaging/ios/client – MRustamzade