0

FCM通過fcm控制檯發送通知到我的ios設備,但是來自php服務它沒有發送通知。我想使用FCM將通知發送到我的應用程序。我在php中實現了web服務,將消息發送到我的應用程序服務器上的應用程序。我爲此創建了4項服務。FCM通過fcm控制檯發送通知到我的ios設備,但是從php服務發送通知沒有發送通知

  1. group_create.php
  2. device_add.php
  3. device_remove.php
  4. send_comments.php

創建組,我得到的通知成功的關鍵,並與FCM註冊registraion ID後面。當我使用通知鍵調用send_comments.php時,它會返回帶有{「success」的json數據:1,「failure」:0}。但我沒有得到任何通知我的ios。我已正確實施所有方法。它適用於fcm控制檯,但不適用於PHP服務。任何人都可以知道這件事。我附上所有4個PHP文件。請幫幫我。

group_create.php

<?php 
$url = 'https://android.googleapis.com/gcm/notification'; 

$notification_key_name = $_REQUEST['notification_key_name']; 
$regid = $_REQUEST['regid']; 
    $fields = array(
     "operation"=>"create", 
     "notification_key_name"=>$notification_key_name, 
     "registration_ids"=> array($regid) 
); 
$fields = json_encode($fields); 

$headers = array (
     "Authorization:key=A************************", 
     "Content-Type:application/json", 
     "project_id:78508******" 
); 

$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_POSTFIELDS, $fields); 

$result = curl_exec ($ch); 

//echo $result; 
$res_notification_key = json_decode($result,true); 

if(array_key_exists('notification_key', $res_notification_key)){ 

$notification_key = $res_notification_key['notification_key']; 

echo $notification_key; 

} 

else{ 

echo $result; 

} 
curl_close ($ch); 
?> 

device_add.php

<?php 

$senderId = "785********"; 
$notification_key_name= $_REQUEST['notification_key_name']; 
$reg_id = $_REQUEST['regid']; 
$notification_key = $_REQUEST['not_key']; 
$apiKey = 

$url = 'https://android.googleapis.com/gcm/notification'; 

    $headers = array (
     "Accept:application/json", 
     "Authorization:key=A******************", 
     "Content-Type:application/json", 
     "project_id:78508*****" 
); 


$fields = array(
     "operation"=>"add", 
     "notification_key_name"=> $notification_key_name, 
     "registration_ids"=> array($reg_id), 
     "notification_key"=>$notification_key 
); 
$fields = json_encode($fields); 

$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_POSTFIELDS, $fields); 


$result = curl_exec ($ch); 

echo $result; 
$res_notification_key = json_decode($result,true); 

if(array_key_exists('notification_key', $res_notification_key)){ 


$notification_key = $res_notification_key['notification_key']; 

echo $notification_key; 

} 

else{ 

echo $result; 

} 
curl_close ($ch); 
?> 

device_remove.php

<?php 

$senderId = "78508*****"; 
$notification_key_name= $_REQUEST['notification_key_name']; 
$reg_id = $_REQUEST['regid']; 
$notification_key = $_REQUEST['not_key']; 
$apiKey = 

$url = 'https://android.googleapis.com/gcm/notification'; 

    $headers = array (
     "Accept:application/json", 
     "Authorization:key=A***********", 
     "Content-Type:application/json", 
     "project_id:78508*****" 
); 


$fields = array(
     "operation"=>"remove", 
     "notification_key_name"=> $notification_key_name, 
     "registration_ids"=> array($reg_id), 
     "notification_key"=>$notification_key 
); 
$fields = json_encode($fields); 

$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_POSTFIELDS, $fields); 


$result = curl_exec ($ch); 

echo $result; 
$res_notification_key = json_decode($result,true); 



if(array_key_exists('notification_key', $res_notification_key)){ 


$notification_key = $res_notification_key['notification_key']; 

echo $notification_key; 

} 

else{ 

echo $result; 

} 
curl_close ($ch); 
?> 

send_comments.php

<?php 

$senderId = "78508*****"; 
$notification_key = $_REQUEST['not_key']; 

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

    $headers = array (
     "Authorization:key=A*****************", 
     "Content-Type:application/json", 


); 

$msg = array("hello"=>"This is a Firebase Cloud Messaging Device Group Message!"); 

$msg_dict = json_encode($msg); 

//echo $msg_dict; 

$fields = array(
     "to"=>$notification_key, 
     "data"=>array(
      "message" => "hell", 

    ), 
); 
$fields = json_encode($fields); 

$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_POSTFIELDS, $fields); 

$result = curl_exec ($ch); 

echo $result; 
$res_notification_key = json_decode($result,true); 

curl_close ($ch); 
?> 
+0

檢查此問題http://stackoverflow.com/questions/38479668/firebase-api-is-not-sending-push-notifications-when-using-the-api/ –

回答

0

的APN如下的格式

{"aps":{"alert":"Testing.. (0)","badge":1,"sound":"default"}} 

{ 
"aps":{ 
"alert":"message here" 
} 
} 

如果從服務器側發送的格式是這樣的,那麼只有iPhone將顯示通知,其他明智的通知數據將僅在控制檯中通過

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage   *)remoteMessage { 
// Print full message 
NSLog(@"Notification :%@", remoteMessage.appData); 
} 

但不顯示在通知列表中。