wangz
wangz

Reputation: 61

firebase cloud messaging (using app client)

Google said FCM is free. What is this mean? Can anyone explain that?

enter image description here

registration_ids:

This parameter specifies a list of devices (registration tokens, or IDs) receiving a multicast message. It must contain at least 1 and at most 1000 registration tokens.

At most 1000? What if i have 2000 users(tokens).

One more problem. there is some mistake in my php script, i could only send noticication to one token(top most row token from taken), plse check my send.php:

table is simple only 1 column with list of tokens.

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

Please help I am stuck with my project.

Upvotes: 0

Views: 217

Answers (2)

Hirendrasinh S. Rathod
Hirendrasinh S. Rathod

Reputation: 834

Please review bellow example, and also take care of parameters sent to FCM server. 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 );
}

Upvotes: 1

AAryan
AAryan

Reputation: 20140

Yes Firebase is free for FCM service.

registration_ids is a ID that is generated by device(Mobile) and send to server, if your app using Firebase.

Firebase use that ID and send any type of notification to number of devices.

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

Upvotes: 0

Related Questions