9

迁移到Firebase后,我测试了通过使用Firebase控制台发送通知的工作正常,但我需要每天通知特定时间,以便不使用Firebase控制台,而是使用我以前的cron作业每天发送通知。我将https://android.googleapis.com/gcm/send更改为https://fcm.googleapis.com/fcm/send,但我的设备未收到任何通知。如何在服务器端实现firebase云消息传递?

有什么办法解决这个问题吗?或者我错过了什么?我的cron作业对于仍在使用GCM的设备工作正常。

这里是我的代码

function sendNotificationFCM($apiKey, $registrationIDs, $messageText,$id) { 


$headers = array(
     'Content-Type:application/json', 
     'Authorization:key=' . $apiKey 
); 

$message = array(
     'registration_ids' => $registrationIDs, 
     'data' => array(
       "message" => $messageText, 
       "id" => $id, 
     ), 
); 


$ch = curl_init(); 

curl_setopt_array($ch, array(
     CURLOPT_URL => 'https://fcm.googleapis.com/fcm/send', 
     CURLOPT_HTTPHEADER => $headers, 
     CURLOPT_POST => true, 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_POSTFIELDS => json_encode($message) 
)); 

$response = curl_exec($ch); 
curl_close($ch); 

return $response; 

}

+1

@McAwesomville从我读过的https://开头火力点。 google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol它表示要发送消息,应用程序服务器i发出POST请求。例如:'https.fcm.googleapis.com/fcm/send' – natsumiyu

+0

@McAwesomville它对使用什么感到困惑:(但我会尝试它 – natsumiyu

+0

我会尽量环顾四周,让我知道如果我找到东西:) :) –

回答

9

我在我的json中添加了notification对象。 我发现在我的remoteMessage.getNotification().getBody()中它返回null,这就是为什么它不会收到我的cron发送的任何通知。

编辑

这里是我的JSON对象

$message = array(
      'registration_ids' => $registrationIDs, 
      'notification' => array(
            "title" => $id, 
            "body" => $messageText, 
            "icon" => "name_of_icon"), 
      'data' => array(
        "message" => $messageText, 
        "id" => $id, 
      ), 
    ); 
+0

如果有效,你可以分享你发送的JSON对象吗? – Blacksad

+0

@Blacksad我添加了我的json对象 – natsumiyu

+0

@mori感谢一吨 –

3

除了网址更改为以下几点:

https://fcm.googleapis.com/fcm/send 

您也可以改变你的方式发送请求数据:

Content-Type:application/json 
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA 

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", // "to" replaces "registration_ids" of gcm in fcm 
    "data" : { 
    ... 
    }, 
} 

C剔除this complete guide

+0

是'message_id'必需的吗? – natsumiyu

+0

如果您只使用HTTP请求,则不需要,如果您正在使用'XMPP'实现聊天类功能。 – astuter

+0

我有一个错误“字段”为“必须是JSON字符串:” – natsumiyu

相关问题