Add a new notification when a push notification receives and replaces the old one

I am using push notification in my application. I used to display a notification when a push notification arrives. If I send another notification (without deleting the previous notification), it replaces the new notification. Please give me a solution very quickly, which is very necessary for me. And this is my code

<?php
// API access key from Google API Console
 	function sendNotification($token,$message){
 		$url = 'https://android.googleapis.com/gcm/send';
 		$msg = array
          (
		'body' 	=> 'T...thoihgcggc',
		'title'	=> 'Dexter Studio 123',
		'subtitle'=> 'This is a subtitle. subtitle',
         'tickerText'=> 'Ticker text here...Ticker text here...Ticker text here',
        'vibrate'=>1,
         'sound'=>'default',
         'tag'=>'hellesjhasvhd',
         'badge'=>0,
         'ID'=>12,
             	
          );
 		$fields = array(
            'registration_ids' => $token, // HERE IT IS
            'data' => $msg
        );
 		
 		define("GOOGLE_API_KEY", "Apikey"); 
 		$headers = array(
        'Authorization: key='. GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
 		//print_r($headers);

 		$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 ) );
		$result = curl_exec($ch );
		echo $result;
		curl_close( $ch );
 	}

 
 	$conn = mysqli_connect("hostname","usrname","password","db");
 	$query = "SELECT * from tbltoken";
 	$token = array();
 	$results = mysqli_query($conn,$query);

 		if(mysqli_num_rows($results)>0 ){
 			while($row = mysqli_fetch_assoc($results)){
 				$token[] = $row["Token"]; 
 			}
 		}
 		//print_r($token);

	mysqli_close($conn); 

	$message = array("message" => "FCM push notification" );
	$message_status = sendNotification($token,$message);
	echo $message_status;
Run codeHide result
+4
source share

Source: https://habr.com/ru/post/1695507/


All Articles