【问题标题】:Firebase Cloud Messaging - invalidRegistration (php)Firebase 云消息传递 - invalidRegistration (php)
【发布时间】:2018-02-06 14:44:17
【问题描述】:

我无法向我的 Firebase 项目中的主题发送推送通知。如果我运行 php 脚本,我会给我错误 'invalidRegistration'

php 脚本:

<?php


$topic = "/topics/info";
$fcm_server_url = "https://fcm.googleapis.com/fcm/send";

$title = utf8_encode("AleXoTroN");
$content_text = utf8_encode("AleXoTroN");
$color_code = utf8_encode("#2baa7f");


$httpheader = array('Content_Type:application/json', 'Authorization:key=***');
$msg = array('to' => $topic, 'data' => array('title' => $title, 'content_text' => $content_text, 'color' => $color_code));

$curl_connection = curl_init();
curl_setopt($curl_connection, CURLOPT_URL, $fcm_server_url);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $msg);
$answerFromServer = curl_exec($curl_connection);
curl_close($curl_connection);

echo "Server:<br/>".$answerFromServer;

?>

以及我的应用中FCM Instance ID服务的代码;

public class FCM_IIDS extends FirebaseInstanceIdService {

    final String token_preference_key = "fcm_token";

    final static String infoTopicName = "info";

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.edit().putString(token_preference_key, FirebaseInstanceId.getInstance().getToken()).apply();


        FirebaseMessaging.getInstance().subscribeToTopic(infoTopicName);
    }
}

【问题讨论】:

    标签: php android http firebase-cloud-messaging


    【解决方案1】:

    您没有将消息编码为 json。

    在将其发送到 FCM 之前使用 json_encode($msg)。

    【讨论】:

    • 所以,根据文档:firebase.google.com/docs/cloud-messaging/http-server-ref(表 9)Check that the request contains a registration token (in the registration_id in a plain text message, or in the to or registration_ids field in JSON).
    • 但是我如何获得这个令牌?我可以使用它将通知发送到注册到我的主题的所有设备吗?
    • 您之前是否尝试过使用 Firebase 控制台向此频道发送通知?
    • 是的,行得通。我可以在控制台上使用通知 ui 发送通知,但我的问题是:如果应用程序处于后台或关闭状态,firebase 会自动显示不可自定义的预配置通知。那是因为我想用脚本发送通知。
    • @AleXoTroN 所以使用notification 字段发送将显示给用户的消息。
    猜你喜欢
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    相关资源
    最近更新 更多