firebase云消息传递(使用应用程序客户端)
问题描述:
Google表示FCM是免费的。这是什么意思?任何人都可以解释吗?firebase云消息传递(使用应用程序客户端)
registration_ids:
此参数指定装置接收多播消息的列表(注册标记,或ID)。它必须包含至少1个和最多1000个注册令牌。
最多1000?如果我有2000个用户(令牌)会怎么样。
还有一个问题。在我的PHP脚本中有一些错误,我只能发送一个令牌通知(最上面的行令牌从采取),plse检查我的send.php:
表是简单的只有1列与令牌列表。
<?php
require "info.php";
$message = $_POST["message"];
$title = $_POST["title"];
$path_to_fcm = "https://fcm.googleapis.com/fcm/send";
$server_key = "*******************************";
$sql="select token from fcm";
$result = mysqli_query($con,$sql);
$column = mysqli_fetch_row($result);
$key=$column[0];
$headers=array('Authorization:key=' .$server_key,
'content-Type:application/json');
$fields=array('registration_ids'=>$key,
'notification'=>array('title'=>$title,'body'=>$message));
$payload = json_encode($fields);
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session, CURLOPT_POST, true);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
$result=curl_exec($curl_session);
curl_close($curl_session);
mysqli_close($con);
?>
请帮我卡住我的项目。
答
是的Firebase是免费的FCM服务。
如果您的应用使用Firebase,registration_ids是由设备(Mobile)生成并发送到服务器的ID。
Firebase使用该ID并向任何设备发送任何类型的通知。
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
/**
* Persist token to third-party servers.
*
* Modify this method to associate the user's FCM InstanceID token with any server-side account
* maintained by your application.
*
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
// Add custom implementation, as needed.
}
}
答
请查看下面的示例,并照顾发送到FCM服务器的参数。 like'to','notification','data'
$registrationIds=array();
//select user RegistrationIDs/Token from DB
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sql="SELECT `reg_id` from users;";
$result=mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)) {
array_push($registrationIds,$row["reg_id"]);
}
mysqli_close($con);
$msg = array(
'body' => "My First Blog Description",
'title' => "My First Blog"
);
$noti_key='YOUR_FCM_REGISTRATION_KEY';
$registrationIds_chunk=array_chunk($registrationIds,1000);
foreach($registrationIds_chunk as $single_chunk){
if(count($single_chunk)==1){
$fields = array
(
'to'=>$single_chunk[0],
'notification'=> $msg
);
}else{
$fields = array
(
'registration_ids'=>$single_chunk,
'notification'=> $msg
);
}
$headers = array
(
'Authorization: key='.$noti_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_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields, true));
$result = curl_exec($ch);
curl_close($ch);
}
请限制自己每个帖子一个问题。 –