Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications show duplicates in background state #1173

Open
baronha opened this issue Dec 18, 2024 · 5 comments
Open

Notifications show duplicates in background state #1173

baronha opened this issue Dec 18, 2024 · 5 comments

Comments

@baronha
Copy link

baronha commented Dec 18, 2024

I saw this issue #690, #767 and I don't know how to completely solve the above problem when displaying notification from Firebase's setBackgroundMessageHandler. How is this resolved in version 7.0.0+ on notifee?
Before that I was still able to use firebase's onNotificationOpenedApp api and it worked fine.
https://rnfirebase.io/messaging/usage#background--quit-state-messages

My code:

messaging().setBackgroundMessageHandler(
  async (remoteMessage) => await onDisplayNotification(remoteMessage)
);
const onDisplayNotification = async (remoteMessage) => {
  const payload = remoteMessage?.notification;
  const title = payload?.title;
  if (!title) return;

  await notifee.createChannel({
    id: "default",
    name: "ELearning Channel",
    sound: "noti",
    vibration: true,
    importance: AndroidImportance.HIGH,
  });

  await notifee.displayNotification({
    title,
    body: payload?.body ?? "Có một thông báo mới",
    data: remoteMessage?.data,
    android: {
      channelId: "default",
      sound: "noti",
      // importance: AndroidImportance.HIGH,
      category: AndroidCategory.MESSAGE,
      visibility: AndroidVisibility.PUBLIC,
      smallIcon: "notification_icon",
      circularLargeIcon: true,
      largeIcon: 'notification_icon',

      pressAction: {
        launchActivity: "default",
        id: "default",
      },
    },
    ios: {
      // ...
    },
  });
};

Result

Screenshot 2024-12-18 at 14 18 59

@ketanuengage
Copy link

@baronha Its happening for me as well but only in iOS. Did you find any solution?

@sbaiahmed1
Copy link

+1

@pavelustenko
Copy link

In my experience, there was a case where I sent a notification object along with data. So one notification was created by Android automatically, and the second was created by setBackgroundMessageHandler. So I switched to data-only notifications.

@abdulwahid211
Copy link

abdulwahid211 commented Dec 25, 2024

I found a little hack to make sure duplicates are not sent. It seems work for me by adding message id to each notification, try the following

On your backend server (if you are using one), i'm using c#

public async Task SendNotificationAsync(NotificationPayload notificationPayload, string token)
    {
        var message = new Message()
        {
            Data = new Dictionary<string, string>()
            {
                 { "id", Guid.NewGuid().ToString() },
                 { "title", notificationPayload.Title },
                 { "body", notificationPayload.Body },
            },
            Token = token,
        };

        try
        {
            string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
            Console.WriteLine("Successfully sent message: " + response);
    
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error sending message: " + ex.Message);
            throw;
        }
    }
// Generate an Id for each notification message, such as `{ "id", Guid.NewGuid().ToString() },
    // On your client side (react native), 

    messaging().onMessage(async (remoteMessage) => {

  if (remoteMessage.data && remoteMessage.data['title']) {
    await notifee.displayNotification({
      id: remoteMessage.messageId,
      title: String(remoteMessage.data['title']),
      body: String(remoteMessage.data['body']),
      android: {
        channelId: '1',
      },
    });
  }
});

Add id such as id: remoteMessage.messageId,

Hope that makes sense to everyone :)

@anuraguengage
Copy link

anuraguengage commented Dec 26, 2024

In my experience, there was a case where I sent a notification object along with data. So one notification was created by Android automatically, and the second was created by setBackgroundMessageHandler. So I switched to data-only notifications.

tried sending data only notifications and it works fine for background and foreground states, but cant receive notifications in quit state. How did you manage to make it work for when app is closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants