如何在服务器端实现firebase云消息传递?

nat*_*iyu 11 cron android firebase google-cloud-messaging firebase-cloud-messaging

迁移到Firebase后,我测试了使用firebase控制台发送通知它工作正常,但我需要在特定时间发送每日通知,因此我不使用firebase控制台而是使用我以前的cron作业每天发送通知.我改变了https://android.googleapis.com/gcm/sendhttps://fcm.googleapis.com/fcm/send,但我的设备没有收到任何通知.

有什么方法可以解决这个问题吗?或者我错过了什么?我的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;
}
Run Code Online (Sandbox Code Playgroud)

}

nat*_*iyu 12

notification在json中添加了对象.我发现在我的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,
            ),
    );
Run Code Online (Sandbox Code Playgroud)


ast*_*ter 8

除了将网址更改为以下内容:

https://fcm.googleapis.com/fcm/send
Run Code Online (Sandbox Code Playgroud)

您还必须更改发送请求数据的方式:

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

 {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", // "to" replaces "registration_ids" of gcm in fcm
   "data" : {
   ...
  },
}
Run Code Online (Sandbox Code Playgroud)

查看完整的指南