Firebase 推送通知不会替换现有通知

Firebase Push Notification Doesn't Replace the Exisiting Notification

我正在创建一个使用 firebase 发送推送通知的聊天应用程序 当我的 chatApp 进入后台并连续发送推送通知时,它每次都会生成新通知,因为我已经为它创建了一个唯一的通知 ID。

我想分组到通知或更新现有通知。

Image that Doesnt Group Firebase Push Notifications

这是我的 Firebase 消息服务代码

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 脚本 发送到 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;
}

要更新现有通知 notify_id 必须与旧通知相同。 如果 notify_id 将被更改,它将生成新通知,不会更新现有通知。

我正在使用以下代码检查消息是否包含数据负载或通知负载(通知负载包含来自 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");
}

为了替换 notify_id 必须与之前的相同。使用一些常数值。

[修复]

所以 PHP 脚本有问题

我使用了"notification"作为发送通知的参数 我将其更改为 "data" 这解决了我的问题

这里是PHP脚本

 $body = array("to"=>$token."", 
              "data" => array(
                    "title" => $title ,
                    "body"=> $message,
                    "click_action"=>$click_action,
                    'vibrate'   => 1,
                    'sound'     => "coin",
                    'largeIcon' => 'large_icon',
                    'smallIcon' => 'small_icon'
                    )
            );

MyFirebaseMessangingClass

 Intent i = new Intent(remoteMessage.getData().get("click_action"));
    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.getData().get("title"))
                .setContentText(remoteMessage.getData().get("body"))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(notification)
                .setNumber(no_of_messages)
                .setContentIntent(pendingIntent);
        no_of_messages++;
    } else {
        builder.setContentTitle("Eaziche | "+no_of_messages+" New Messages")
                .setNumber(no_of_messages)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(notification)
                .setNumber(no_of_messages)
                .setContentIntent(pendingIntent)
                .setContentText(remoteMessage.getData().get("title"));
    }

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(notify_id, builder.build()); // notify_id = 12121