发送POST REQUEST Firebase云消息传递

EDITED. Solved at the end of the post.

I´m trying to update GCM to FCM and everything works perfect but the connection with the Firebase API.

In GCM I sent had this code from my PHP script:

define( 'API_ACCESS_KEY', 'myapikeynumber');
$registrationIds = array($registrationID);
$msg = array
    (
    'message'         => $message,
    'title'           => 'You have new notification(s)',
    'subtitle'        => 'Please click to view your notification(s)',
    'user_id'         => $user_id,
    'user_type'       => $user_type,
    'notification_id' => $notification_id
    );
$fields = array
    (
    'registration_ids'  => $registrationIds,
    'data'              => $msg,
    'time_to_live'      => 3
    );
$headers = array
    (
    'Authorization: key='.API_ACCESS_KEY,
    'Content-Type: application/json'
    );
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt( $ch,CURLOPT_POST, true);
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch );
curl_close( $ch );
echo $result;

But now don´t know which fields or values I need send to the https://fcm.googleapis.com/fcm/send.

Any help?


SOLUTION

The problem was here:

$registrationIds = array($registrationID);
$fields = array
    (
    'registration_ids'  => $registrationIds,
    'data'              => $msg,
    'time_to_live'      => 3
    );

Because $registrationsIds had not JSON structure.

So the code is nearly the same, just change that and the URL of the API to https://fcm.googleapis.com/fcm/send.

doucong1268
doucong1268 是。事实上,你甚至不需要改变网址。唯一改变的是android客户端实现
4 年多之前 回复
dsrnwngq411594
dsrnwngq411594 您的意思是将https://android.googleapis.com/gcm/send改为https://fcm.googleapis.com/fcm/send
4 年多之前 回复
douyin2962
douyin2962 FCM与重新命名的GCM相同
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问