我在向Firebase项目中的主题发送推送通知时遇到了问题。如果我运行php脚本,我会显示错误'validRegistration'
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实例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);
}
}
您没有将消息编码为json。
在将json_encode($msg)
发送给FCM之前,请先使用它。