0

I've an app which receives notifications and have been trying to build the badge count locally on the app.

Within the App Delegate on iOS, I have the following

   
int notifcount = Preferences.Get("notifcount", 0);
                
UIApplication.SharedApplication.ApplicationIconBadgeNumber = notifcount +1;

        
Preferences.Set("notifcount", notifcount + 1);

This is placed in the methods DidReceiveRemoteNotification & WillPresentNotification.

Everything seems to work fine in debug mode - I receive notifications and the badge increments. However, when I release the app on Test Flight the behaviour changes - some users report the badge working fine, some get the badge incrementing sometimes and some don't get it at all.

I know ideally the badge would be passed along with the payload (but the app doesn't have user accounts yet).

My question is there limitations to Xamarin Essentials Preferences that stop it being used this way or something within iOS that could be causing this behaviour?

Thanks!

Raymond Dillon
  • 197
  • 1
  • 18
  • Could you provide some of your code for push notifications? Will those users whose badge not incremented receive notifications normally? – Zack Nov 09 '22 at 02:42
  • Hi - the notifications are received and displayed perfectly all the time no problem. Seems to be an issue with the storing of the int value for the badge count using Xamarin Essentials Preferences. Is there another way to maintain a local count in app that persists across phone reboot , app being killed & backgrounded etc? – Raymond Dillon Nov 09 '22 at 17:15
  • @DongzhiWang-MSFT - the badge increment code implemented in appdelegate.cs. Does it always run - see my related question at https://stackoverflow.com/questions/74382456/app-delegate-behaviour-when-app-is-in-different-states – Raymond Dillon Nov 09 '22 at 23:48
  • Did this answer help you? https://stackoverflow.com/questions/50005433/how-to-show-badge-count-in-xamarin-ios-when-application-in-background#:~:text=1-,Answer,-Sorted%20by%3A – Zack Nov 10 '22 at 06:47
  • @DongzhiWang-MSFT It does a little bit - I have that code in - I think based on particular app states my code to increment the badge count isn't called in appdelegate.cs - this is based on the answer at https://stackoverflow.com/questions/19068762/will-ios-launch-my-app-into-the-background-if-it-was-force-quit-by-the-user where it says "However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again." – Raymond Dillon Nov 10 '22 at 10:54

1 Answers1

1

For iOS, the badge count is managed by iOS itself, and it is completely OS dependent when the app is in the background or killed. You can send the badge count in the payload of the push notification, but you should do the calculation on the server side.

Zack
  • 1,255
  • 1
  • 2
  • 5
  • Thanks! I was just wondering is there a technical reason why it can't be done locally? Is it because the app delegate may not necessarily run depending on the app state? – Raymond Dillon Nov 11 '22 at 10:41
  • You can check the official documentation of the Xamarin.iOS [lifecycle](https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/application-lifecycle-demo), and you can try adding breakpoints in AppDelegate to see what happens in different states. – Zack Nov 15 '22 at 02:26