flutter_app_badger icon indicating copy to clipboard operation
flutter_app_badger copied to clipboard

change badge count in background

Open MuraliThangavel opened this issue 3 years ago • 23 comments

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

MuraliThangavel avatar Jan 25 '22 08:01 MuraliThangavel

+1

subtyven avatar Feb 15 '22 13:02 subtyven

@g123k please check this

mminhlequang avatar Feb 22 '22 03:02 mminhlequang

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);
}

GeceGibi avatar Mar 14 '22 13:03 GeceGibi

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);
}

subtyven avatar Mar 18 '22 09:03 subtyven

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

GeceGibi avatar Mar 18 '22 09:03 GeceGibi

Ok anyway the problem for me is same as #57

subtyven avatar Mar 18 '22 09:03 subtyven

If background handler not working. U should add

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

in your plist file.

GeceGibi avatar Mar 18 '22 09:03 GeceGibi

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?

UsamaKarim avatar Apr 08 '22 01:04 UsamaKarim

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.

GeceGibi avatar Apr 08 '22 10:04 GeceGibi

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.

aurangzaibsiddiqui avatar May 01 '22 13:05 aurangzaibsiddiqui

@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']));
  }
}

roshandroids avatar Jul 04 '22 06:07 roshandroids

@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.

UsamaKarim avatar Jul 04 '22 09:07 UsamaKarim

@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. 🍻

roshandroids avatar Jul 04 '22 10:07 roshandroids

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(); }

aurangzaibsiddiqui avatar Jul 04 '22 11:07 aurangzaibsiddiqui

@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.

UsamaKarim avatar Jul 04 '22 15:07 UsamaKarim

did anyone solve the issue?

shahmirzali49 avatar Jan 25 '23 08:01 shahmirzali49

did anyone solve the issue? could you check this

roshandroids avatar Jan 25 '23 08:01 roshandroids

did anyone solve the issue? could you check this

You mean content available property?

shahmirzali49 avatar Jan 25 '23 09:01 shahmirzali49

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

roshandroids avatar Jan 25 '23 11:01 roshandroids

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

Can someone provide any advice and help?

https://github.com/g123k/flutter_app_badger/issues/71

I really appreciate any advice and insights you could offer.

gujingc avatar Feb 17 '23 08:02 gujingc

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

goldenmoon67 avatar Feb 28 '23 20:02 goldenmoon67

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.

raymondk25 avatar May 19 '23 02:05 raymondk25

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

goldenmoon67 avatar May 19 '23 12:05 goldenmoon67