2017-02-11 138 views
0

Google表示FCM是免费的。这是什么意思?任何人都可以解释吗?firebase云消息传递(使用应用程序客户端)

enter image description here

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); 
?> 

请帮我卡住我的项目。

+1

请限制自己每个帖子一个问题。 –

回答

0

是的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. 
    } 
} 
1

请查看下面的示例,并照顾发送到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); 
} 
+0

Plse用给定的参数重写上面的代码,我没有经验开发人员,所以它让我感到困惑。 $令牌(PLSE从表table_name的FCM提取与1列标记) $标题 $消息 (没有到需要通过额外的数据,并发出通知) 使用数据库来获取令牌 PLSE PLSE – wangz

+0

@wangz请查看编辑。 –

+0

plse排除$ data(我不想发送数据)..而是包括$ title(通知标题).. – wangz

相关问题