Open MuraliThangavel opened 2 years ago
+1
@g123k please check this
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);
}
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);
}
Ups sorry my bad. Native = FlutterAppBadger
btw. I'm updated my comment.
Ok anyway the problem for me is same as #57
If background handler not working. U should add
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
in your plist file.
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?
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.
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.
@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']));
}
}
@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 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. 🍻
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
// 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(); }
@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.
did anyone solve the issue?
did anyone solve the issue? could you check this
did anyone solve the issue? could you check this
You mean content available property?
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
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.
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
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.
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
how to change badge count when app is in background. for example, when i receive push notification, i have to increase the badge count