简体   繁体   English

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

[英]How to implement firebase cloud messaging in server side?

After migrating to Firebase, i tested sending notification by using the firebase console it works fine, but i need a daily notification on a specific time so instead of using the firebase console i use my former cron job to send notification daily. 迁移到Firebase后,我测试了使用firebase控制台发送通知它工作正常,但我需要在特定时间发送每日通知,因此我不使用firebase控制台而是使用我以前的cron作业每天发送通知。 I changed https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send but my device doesn't receive any notification. 我将https://android.googleapis.com/gcm/send更改为https://fcm.googleapis.com/fcm/send但我的设备未收到任何通知。

Is there any way to solve this? 有什么方法可以解决这个问题吗? Or did I miss anything? 或者我错过了什么? my cron job is working fine for my devices that is still using GCM. 我的cron工作对我仍在使用GCM的设备工作正常。

Here's my code 这是我的代码

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;
}

I added notification object in my json. 我在json中添加了notification对象。 I found out that in my remoteMessage.getNotification().getBody() it returns null that's why it doesn't receive any notification send by my cron. 我发现在我的remoteMessage.getNotification().getBody()它返回null,这就是为什么它没有收到我的cron发送的任何通知。

Edit 编辑

Here's my json object 这是我的json对象

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

Apart from changing the url to following: 除了将网址更改为以下内容:

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

You also have to change the way you send request data: 您还必须更改发送请求数据的方式:

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

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

Check out this complete guide . 查看完整指南

I am not able to comment directly in the selected answer. 我无法直接在所选答案中发表评论。 Take in consideration that the json message changes according to what you want/need to receive in the recipient os (android/ios). 考虑到json消息根据你想要/需要在收件人os(android / ios)中接收的内容而改变。

eg for android to get the notifications when the app is in background you need to add the data json in your request. 例如,当应用程序处于后台时,如果要获取通知,则需要在请求中添加数据 json。

For iOS the data key is not needed, but you must have the notification key along with isContentAvailble flag set to true and priority set to high. 对于iOS,不需要数据键,但必须将通知键和isContentAvailble标志设置为true并将优先级设置为高。

In the selected answer as valid, you use registration_ids key should be only used when you are sending the notification to more than one device, maybe you should take a look to the topics notifications. 在选定的答案中有效,您只能在将通知发送到多个设备时使用registration_ids键,也许您应该查看主题通知。

try use this :) 试试用这个:)

Python client for FCM FCM的Python客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM