firebase云消息传递(使用app客户端)

时间:2017-02-11 09:23:47

标签: php android firebase firebase-cloud-messaging

谷歌称FCM是免费的。这是什么意思?任何人都可以解释一下吗?

enter image description here

registration_ids:

此参数指定接收多播消息的设备列表(注册令牌或ID)。它必须包含至少1个,最多1000个注册令牌。

最多1000?如果我有2000个用户(令牌)怎么办。

还有一个问题。在我的PHP脚本中有一些错误,我只能向一个令牌发送通知(从最上面的行令牌中取出),请检查我的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);
?>

请帮助我坚持我的项目。

2 个答案:

答案 0 :(得分:1)

请查看以下示例,并注意发送到FCM服务器的参数。喜欢&#39;到&#39;,&#39;通知&#39;,&#39;数据&#39;

$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 );
}

答案 1 :(得分:0)

是Firebase可免费使用FCM服务。

registration_ids是设备(移动设备)生成的ID,如果您的应用使用Firebase,则会发送到服务器。

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.
    }
}