g123k / flutter_app_badger

Support to update the app badge on the launcher (both for Android and iOS)
https://pub.dev/packages/flutter_app_badger
Apache License 2.0
307 stars 172 forks source link

change badge count in background #58

Open MuraliThangavel opened 2 years ago

MuraliThangavel commented 2 years ago

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 commented 2 years ago

+1

mminhlequang commented 2 years ago

@g123k please check this

GeceGibi commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

subtyven commented 2 years ago

Ok anyway the problem for me is same as #57

GeceGibi commented 2 years ago

If background handler not working. U should add

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

in your plist file.

UsamaKarim commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

@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 commented 1 year ago

did anyone solve the issue?

roshandroids commented 1 year ago

did anyone solve the issue? could you check this

shahmirzali49 commented 1 year ago

did anyone solve the issue? could you check this

You mean content available property?

roshandroids commented 1 year ago

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 commented 1 year ago

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.

goldenmoon67 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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