androidfirebasepush-notificationnotificationsfirebase-notifications

Firebase Push Notification Doesn't Replace the Exisiting Notification


I'm creating a chat application which send push notifications using firebase as my chatApp goes in background and send push notifications in row it generate new notification everytime as i have created a unique notification id for it.

I want to group to notification or update the existing one.

Image that Doesnt Group Firebase Push Notifications

Here is my Code of Firebase Messaging Service

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public int no_of_messages = 0,i=0;
private int notify_id= 12121; // this was my actual code

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    showNotification(remoteMessage, remoteMessage.getData().get("message"));
    no_of_messages++;
}

private void showNotification(RemoteMessage remoteMessage, String message) {


Intent i = new Intent(remoteMessage.getNotification().getClickAction());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Uri notification = Uri.parse("android.resource://"
            + this.getPackageName() + "/" + R.raw.coin);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    PendingIntent pendingIntent;

    pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    if (no_of_messages == 0) {
        builder.setAutoCancel(true)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setSmallIcon(R.drawable.auto)
                .setSound(notification)
                .setNumber(no_of_messages)
                .setContentIntent(pendingIntent);
    } else {
        builder.setContentTitle(no_of_messages+"New Messages")
                .setNumber(no_of_messages)
                .setContentText(remoteMessage.getNotification().getTitle());
    }

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(notify_id, builder.build());

   }
}

PHP script function for sending to FCM

function send_to_fcm($token,$title,$message,$click_action){

    $body = array("to"=>$token."", 
                  "notification" => array(
                        "title" => $title ,
                        "body"=> $message,
                        "click_action"=>$click_action,
                        'vibrate'   => 1,
                        'sound'     => "coin",
                        'largeIcon' => 'large_icon',
                        'smallIcon' => 'small_icon'
                        )
                );
    echo json_encode($body);
    $header = array("Authorization:key=".FCM_SERVER_KEY,"Content-type:application/json");

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,  FCM_PATH);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));

    $buffer = curl_exec($ch);

    curl_close($ch); 
    //echo $buffer;
}

Solution

  • For update existing notification notify_id must be same as older one. if notify_id will be changed it will generate new notification will not update existing one.

    I am using below code for check message contain data payload or notification payload (notification payload contains notification from PHP)

    if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
                title = remoteMessage.getData().get("title");
                message = remoteMessage.getData().get("body");
                image = remoteMessage.getData().get("icon");
    }
    
            // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
                Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
                title = remoteMessage.getNotification().getTitle();
                message = remoteMessage.getNotification().getBody();
                image = remoteMessage.getData().get("image");
    }