如何将Firebase(fcm)发送给多个用户以及每个用户不同的数据

时间:2019-02-07 09:39:37

标签: php android firebase webview firebase-cloud-messaging

我想将fcm发送给多个用户,但我想向每个用户发送不同的数据,像这样。

$deviceid2 ="da10bCm38LU:APA91bHC-XB";
$deviceid1 = "dKF-20xsI9Y:APA91bE";
$API_ACCESS_KEY='AAAA_';
$registrationIds = array( "$deviceid1","$deviceid2" );
$title1="title1";
$title2="title2";
$message1="message1";
$message2="message2";
$title = array( "$title1","$title2" );
$body = array( "$message1","$message2" );
$fields = array
(
'registration_ids'  => $registrationIds,
'data'   => array(

                'title'     => $title,
                'body'      => $body
)
);

$headers = array

(
'Authorization: key='.$API_ACCESS_KEY,
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch );
curl_close( $ch );

我尝试过,响应是成功的,但我的手机没有显示任何通知。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

然后,您需要发送与用户一样多的不同FCM消息。您不能有一条针对不同用户的不同消息!