如何在服务器端实现Firebase云消息传递? [英] How to implement firebase cloud messaging in server side?

查看:61
本文介绍了如何在服务器端实现Firebase云消息传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. I changed https://android.googleapis.com/gcm/send to https://fcm.googleapis.com/fcm/send but my device doesn't receive any notification.

有什么办法解决这个问题?还是我错过了什么?我的Cron工作对于仍在使用GCM的设备运行正常.

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.

这是我的代码

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

推荐答案

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

I added notification object in my json. 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.

修改

这是我的json对象

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

这篇关于如何在服务器端实现Firebase云消息传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆