Home > Software engineering >  Set ID in FCM notification
Set ID in FCM notification

Time:01-19

I am developing a tool to send FCM notifications to Android phones. I am using Firebase Cloud Messaging to do this. I send the notifications through PHP and CURL. I would like all the notifications to come out with the same identifier, since every time they are sent, a new one is generated, and I would like it to be replaced.


/*PHP CURL*/

$to="TOKEN USER";
$apiKey="KEY APPLICATION";

$notification= array(
            'title'=>'TITLE',
            'body' => 'BODY',
         /* HERE SHOULD I ADD THE ID? */
);

$ch = curl_init();

    $url="https://fcm.googleapis.com/fcm/send";
    $fields=json_encode(array('to'=>$to,"notification"=>$notification));

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    $headers = array();
    $headers[] = 'Authorization: key ='.$apiKey;
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);

CodePudding user response:

I've tried adding these two options, but it doesn't work either.

     $notification= array(
            'title'=>'Title',
            'body' => 'Body', 
            'channelId' => 'Channel',  
            'android_channel_id' => 'Channel'       
        );

CodePudding user response:

Ok I found the solution, using collapse_key

$fields=json_encode(array('to'=>$to,"notification"=>$notification,"collapse_key"=>"new_messages"));

  $notification= array(
            'title'=>'Title',
            'body' => 'Body', 
            'tag' => 'new_messages'   
        );
  •  Tags:  
  • Related