Is it possible for badge notification to display when I don't use setBadgeCountAsync() at all ?

  1. SDK Version: 43
  2. Platforms(Android/iOS/web/all):Android

I have used expo-notification to support push notification functionality in my application. It works fine but there’s a strange behavior. After I installed the app and (accidentally) enabled badge notification for it, the badge count indicating the number of unread message appeared on app icon whenever I send a notification. How is this possible? I didn’t use setBadgeCountAsync() anywhere in my code. Thanks

Hey @spellscripter, the badge will be updated if your notification payload you deliver has the badge field. See Sending Notifications with Expo's Push API - Expo Documentation

The setBadgeCountAsync method is available for more-nuanced manipulation of the badge count value within your application.

Cheers,
Adam

I think there’s something not very clear in the documentation @adamjnav
If I understand you well (and after some weeks struggling on this badge issue), on iOS : we must handle the number appearing on the badge count manually on the server side and on the client side.

It’s not written anywhere and I see a lot of people lost on this subject.

Here’s how we handle it (I don’t know if it’s the good way or not) :

  • When sending a notification from your server using Expo Push API, you must set a badge key. The value of this key will be the number of unreadNotifications in total for one user (you can compute it as you want to).
  • On your client, you must define a behavior that is going to decrease this number using both expo-notifications methods : setBadgeCountAsync & getBadgeCountAsync.

For example, in our app, we decrease this number when a user clicks on a notification :

export const markAsReadAndUpdateBadgeCount = async (notificationIds: number[]): Promise<void> => {
	await markNotificationsAsRead(notificationIds);
	const currentUnreadNotificationsCount = await Notifications.getBadgeCountAsync();
	await Notifications.setBadgeCountAsync(currentUnreadNotificationsCount - notificationIds.length);
};
useEffect(() => {
  responseListener.current = Notifications.addNotificationResponseReceivedListener(
	({
		notification: {
			request: {
				content: { data },
			},
		},
	}) => await markAsReadAndUpdateBadgeCount([data?.notificationId]);
  );
}, [])

Hope it helps (and I’m not wrong).

Have a nice day,

@yonitou Does this work on android too? I used a slightly different approach but the app badge doesn’t get removed when Notifications.setBadgeCountAsync(0).It goes back to maximum number of the unread notification(e.g There are 4 unread messages at that time.The badge count decreases when I make them as read except for the very last one.Instead of clearing the badge out of the app icon, the badge count goes back to 4 again.) Any idea? thanks.

Hello Adam, is it possible to achieve Android’s behavior on iOS without backend sending badge count?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.