Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

change badge count in background #58

Open
MuraliThangavel opened this issue Jan 25, 2022 · 23 comments
Open

change badge count in background #58

MuraliThangavel opened this issue Jan 25, 2022 · 23 comments

Comments

@MuraliThangavel
Copy link

how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count

@subtyven
Copy link

+1

@mminhlequang
Copy link

@g123k please check this

@GeceGibi
Copy link

GeceGibi commented Mar 14, 2022

how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count

Which library are u using for push notification?

if you are working with firebase_messaging u can use backgoundHandler

It's working for me.

Future<void> _backgroundNotificationHandler(RemoteMessage message) async {
  final preferences = await SharedPreferences.getInstance();
  const key = '@badge_count';

  // Get Last Badge Count
  final count = (preferences.getInt(key) ?? 0) + 1;

  // Update Badge Count
  await preferences.setInt(key, count);

  Native.badgeUpdate(count);
}

@subtyven
Copy link

Hello @GeceGibi ,

What is "Native" ?

My code looks like this :

Future<void> main() async {
  FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
  runApp(child: const MyApp()));
}
Future<void> handleBackgroundMessage(RemoteMessage remoteMessage) async {
  FlutterAppBadger.updateBadgeCount(1);
}

@GeceGibi
Copy link

Ups sorry my bad. Native = FlutterAppBadger btw. I'm updated my comment.

@subtyven
Copy link

Ok anyway the problem for me is same as #57

@GeceGibi
Copy link

GeceGibi commented Mar 18, 2022

If background handler not working. U should add

<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>

in your plist file.

@UsamaKarim
Copy link

UsamaKarim commented Apr 8, 2022

how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count

Which library are u using for push notification?

if you are working with firebase_messaging u can use backgoundHandler

It's working for me.

Future<void> _backgroundNotificationHandler(RemoteMessage message) async {
  final preferences = await SharedPreferences.getInstance();
  const key = '@badge_count';

  // Get Last Badge Count
  final count = (preferences.getInt(key) ?? 0) + 1;

  // Update Badge Count
  await preferences.setInt(key, count);

  Native.badgeUpdate(count);
}

It does only work when you click on the notification? Isn't it?

@GeceGibi
Copy link

GeceGibi commented Apr 8, 2022

how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count

Which library are u using for push notification?
if you are working with firebase_messaging u can use backgoundHandler
It's working for me.

Future<void> _backgroundNotificationHandler(RemoteMessage message) async {
  final preferences = await SharedPreferences.getInstance();
  const key = '@badge_count';

  // Get Last Badge Count
  final count = (preferences.getInt(key) ?? 0) + 1;

  // Update Badge Count
  await preferences.setInt(key, count);

  Native.badgeUpdate(count);
}

It does only work when you click on the notification? Isn't it?

Nope. It's working when app in background.

@aurangzaibsiddiqui
Copy link

how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count

Which library are u using for push notification?
if you are working with firebase_messaging u can use backgoundHandler
It's working for me.

Future<void> _backgroundNotificationHandler(RemoteMessage message) async {
  final preferences = await SharedPreferences.getInstance();
  const key = '@badge_count';

  // Get Last Badge Count
  final count = (preferences.getInt(key) ?? 0) + 1;

  // Update Badge Count
  await preferences.setInt(key, count);

  Native.badgeUpdate(count);
}

It does only work when you click on the notification? Isn't it?

Nope. It's working when app in background.

Hey, so while this is working for me using the onMessage function while the app is in the foreground, it does not seem to be working for me in the backroundHandler. Am I doing something wrong? The notification that I am sending has both a notification component and a data component; would that affect the performance in some way? I am not even sure if my backgroundHandler is being triggerred although it is properly configured. I can see the notifications received in background however.

@roshandroids
Copy link

roshandroids commented Jul 4, 2022

@GeceGibi Following your instructions I was able to make it work while app was in background i.e not terminated and is only minimized and app is in foreground. But This doesn't seems to be working while app is terminated or removed from recent app list in iOS. BTW i am able to receive the notification even if the app is terminated however I am not able to update the badge count. Below is the snippet of how my background handler looks like. Thanks 🙏

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  final _uid = await FirebaseAuth.instance.currentUser?.uid;

  if (_uid != null) {
    final queryData = await FirebaseFirestore.instance
        .collection('conversations')
        .where('uid_chat_partner', isEqualTo: _uid)
        .get();
    var msgCount = 0;
    queryData.docs.forEach((docItem) {
      final count = docItem.data()['user_info'][_uid]['unread_messages'] as int;
      msgCount = count + msgCount;
    });
    FlutterAppBadger.updateBadgeCount(int.parse(message.data['count']));
  }
}

@UsamaKarim
Copy link

@GeceGibi Following your instructions I was able to make it work while app was in background i.e not terminated and is only minimized and app is in foreground. But This doesn't seems to be working while app is terminated or removed from recent app list in iOS. BTW i am able to receive the notification even if the app is terminated however I am not able to update the badge count. Below is the snippet of how my background handler looks like. Thanks 🙏

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  final _uid = await FirebaseAuth.instance.currentUser?.uid;

  if (_uid != null) {
    final queryData = await FirebaseFirestore.instance
        .collection('conversations')
        .where('uid_chat_partner', isEqualTo: _uid)
        .get();
    var msgCount = 0;
    queryData.docs.forEach((docItem) {
      final count = docItem.data()['user_info'][_uid]['unread_messages'] as int;
      msgCount = count + msgCount;
    });
    FlutterAppBadger.updateBadgeCount(int.parse(message.data['count']));
  }
}

It's obvious that it will not work when the app is terminated. As mentioned in the docs here.

  • On iOS, if the user swipes away the application from app Switcher, it must be manually reopened again for background messages to start working again.

@roshandroids
Copy link

roshandroids commented Jul 4, 2022

@GeceGibi Following your instructions I was able to make it work while app was in background i.e not terminated and is only minimized and app is in foreground. But This doesn't seems to be working while app is terminated or removed from recent app list in iOS. BTW i am able to receive the notification even if the app is terminated however I am not able to update the badge count. Below is the snippet of how my background handler looks like. Thanks 🙏

Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  final _uid = await FirebaseAuth.instance.currentUser?.uid;

  if (_uid != null) {
    final queryData = await FirebaseFirestore.instance
        .collection('conversations')
        .where('uid_chat_partner', isEqualTo: _uid)
        .get();
    var msgCount = 0;
    queryData.docs.forEach((docItem) {
      final count = docItem.data()['user_info'][_uid]['unread_messages'] as int;
      msgCount = count + msgCount;
    });
    FlutterAppBadger.updateBadgeCount(int.parse(message.data['count']));
  }
}

It's obvious that it will not work when the app is terminated. As mentioned in the docs here.

  • On iOS, if the user swipes away the application from app Switcher, it must be manually reopened again for background messages to start working again.

@GeceGibi Thanks for the response. I have managed to get it working even if the app is terminated. 🍻

@aurangzaibsiddiqui
Copy link

The following code works perfectly for me. It also stores badge count locally so you don't need to store it anywhere in cloud database.

Future firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final preferences = await SharedPreferences.getInstance();
const key = '@badge_count';

// Get Last Badge Count
final count = (preferences.getInt(key) ?? 0) + 1;

// Update Badge Count
await preferences.setInt(key, count);

FlutterAppBadger.updateBadgeCount(count);
// AwesomeNotifications().incrementGlobalBadgeCounter();

print(message.data);

// await Firebase.initializeApp();
}

@UsamaKarim
Copy link

@GeceGibi Thanks for the response. I have managed to get it working even if the app is terminated. 🍻

Please share your knowledge So others can learn.

@shahmirzali49
Copy link

did anyone solve the issue?

@roshandroids
Copy link

did anyone solve the issue?
could you check this

@shahmirzali49
Copy link

shahmirzali49 commented Jan 25, 2023

did anyone solve the issue?
could you check this

You mean content available property?

@roshandroids
Copy link

did anyone solve the issue?
could you check this

You mean content available property?

Yes, if the issue is iOS specific that might be the problem as I fixed with that

@gujingc
Copy link

gujingc commented Feb 17, 2023

Hello, we are having issue the Android app badge would not reset.

Can someone provide any advice and help?

#71

I really appreciate any advice and insights you could offer.

@goldenmoon67
Copy link

how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count

Which library are u using for push notification?
if you are working with firebase_messaging u can use backgoundHandler
It's working for me.

Future<void> _backgroundNotificationHandler(RemoteMessage message) async {
  final preferences = await SharedPreferences.getInstance();
  const key = '@badge_count';

  // Get Last Badge Count
  final count = (preferences.getInt(key) ?? 0) + 1;

  // Update Badge Count
  await preferences.setInt(key, count);

  Native.badgeUpdate(count);
}

It does only work when you click on the notification? Isn't it?

Nope. It's working when app in background.

this is not working for me. Is there any other settings for ios device. btw if I open my app and send notification from firebase, counter is working. but while app is closed counter is not working. I can take notification but I cant change the count

@raymondk25
Copy link

raymondk25 commented May 19, 2023

this is not working for me. Is there any other settings for ios device. btw if I open my app and send notification from firebase, counter is working. but while app is closed counter is not working. I can take notification but I cant change the count

+1 same, my onBackgroundMessage (background) also not working.

@goldenmoon67
Copy link

this is not working for me. Is there any other settings for ios device. btw if I open my app and send notification from firebase, counter is working. but while app is closed counter is not working. I can take notification but I cant change the count

+1 same, my onBackgroundMessage (background) also not working.

Hi mister. If you are using the firebase push notification, you can send badge count from the firebase push notification service. It is working automatically. I solved this issue just like that. You can check the firabse push notification docs. It would be helpful

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

No branches or pull requests