Using Xamarin Essentials Preferences for Notification Badge Count - xamarin

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!

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.

Related

How to schedule local-notifications when the app in background for iOS in Nativescript -angular

Hai am developing an ios application using Nativescript-angular framework.My app contains local notifications functionality using timer. but I click the home button In my phone the timer was destroyed and notifications not came. Is there anyway to run the timer when my app is in background?
In order show notifications after app is moved to background, you should use BackgroundTask.
If you like to perform any operation even when the app is not running, then you should opt for BackgroundFetch

Location Service is not running in ios and android background for Xamarin

I am developing an app in Xamarin Forms which will show thing live position of the user on a map and all the users having the app can track other users movement.
Right now the app is working fine in android. But in the iOS foreground, android background it stops after some time. It is not at all running in ios background when I put the app in the background it stops showing the location and stops fully.
Please share some idea to keep the app live in background and foreground mode for the whole day.
Generally speaking, mobile OSs will stop your apps, when they are in the background for some time. How strict the OSs are depends on the OS and the version. iOS has "always" been very strict about it, Android has been a bit more loose in older versions, but is quite restrictive in newer versions. Anyway, getting the location in background is quite a common use case and OSs are providing you options to do it nevertheless, if you ask them politely.
iOS
As of iOS 11, there is a distinction between updating the location when in use and when in background. The user can choose which one to grant and you'll have to handle it appropriately.
According to this guide you'll have to add the NSLocationAlways key to your Info.plist to contimue getting the location when in background.
Android
From the Android developer guide
In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently background apps can retrieve the user's current location. Apps can receive location updates only a few times each hour.
Anyway
The system distinguishes between foreground and background apps.
An app is considered to be in the foreground if any of the following is true:
It has a visible activity, whether the activity is started or paused.
It has a foreground service.
...
According to this, you can create a foreground service, see here more about it
For this reason, foreground services must show a status bar notification with a priority of PRIORITY_LOW or higher, which helps ensure that the user is aware of what your app is doing.
So if you are starting a foreground service that is shown in notification bar, you should be able to retrieve updates, even if your apps main Activity is not in foreground.

Can I keep a Watch app running in background?

I know iOS allows background tasks to run and, for example, continue to receive location updates, but is it possible to do this in a watch app?
In Xcode 9.3, I have configured my app for "background modes" and selected location, and that has created for the WatchKitExtension's an Info.plist, an entry for "Required background modes" of "App registers for location updates".
But my watch app still suspends when the screen turns off, and when it is in the dock.
The App Programming Guide for watchOS, however seems to exclude the possibility of running in the background to receive location updates as it only allows background processing for four classes of activity:
Background App Refresh Tasks. Use a WKApplicationRefreshBackgroundTask object to ...
Background Snapshot Refresh Tasks. Use a WKSnapshotRefreshBackgroundTask object to update ...
Background Watch Connectivity Tasks. Use a WKWatchConnectivityRefreshBackgroundTask object to receive data sent by your iOS app ...
Background NSURLSession Tasks. ...
Other posts to SO indicate it's not possible, but proving a negative is difficult, so I'm asking again:
Am I "flogging a dead horse" by trying to keep the watch App operating in the background for receiving location updates, or is Xcode is making promises that WatchOS won't deliver.
I'm delighted to be able to report that the horse I have been flogging for the past two weeks was not dead after all!
I have discovered an additional state in which my watch app will continue to run in the Background which does not require HKWorkoutSession.
These settings did the trick:
locationManager.allowsBackgroundLocationUpdates = true
and in watchKitExtension info.plist:
set UIBackgroundModes (Required background modes) to location (App registers for location updates)
And I repeat: I am NOT using healthKit
Now my app continues to run even when the screen is off and when the app is out of the Dock.
No, it is not possible.
As the WatchKit Programming Guide clearly states, WatchKit apps cannot use background execution except for 3 use cases:
Network operations using URLSession
Playing audio using WKAudioFilePlayer or WKAudioFileQueuePlayer
Run a workout using HKWorkoutSession
You cannot receive location updates in the background, according to the WatchKit Programming Guide, that should be done in the iOS app that is connected to your watchOS application.

Is it possible to create custom Android Wear notifications?

I have been doing lots of research trying to figure out if it is possible to create wear notifications with custom layouts over the past few months, but am struggling to find a solution.
I have tried to implement the following suggestions to no luck:
https://possiblemobile.com/2014/07/create-custom-ongoing-notification-android-wear/
http://android-wear-docs.readthedocs.org/en/latest/sync.html
Custom UI for Android Wear Notifications
Most of what I have read have instructed that in order to do this, I should create an android wear app with a data listener. The idea being the phone would send certain data to the watch (say a string) based on the type of notification it wants to send. I would disable the automatically generated wear notification. In its place, the Android Wear app listener would know what type of notification it is based on the string and display my custom pages for that notification instead.
My first problem I have run into is I have no idea how to debug code like this. Since in Android studio you can only launch the device app or the wear app, but not both at the same time.
The other question I have is, does the Android Wear app run in the background when the Device App is running? Do you have to open the Android Wear App first for the listener to be even working (if so then I guess this is not a solution to custom notifications)?
Is it actually possible to create custom layout android wear notifications?
If you want to use a custom layout for your notifications, you should create the notification on the watch, following Creating Custom Layouts. You can even make the custom notifications clickable, you can add actions that will open activities on the watch, etc.
For that, you can send a message from your phone to your watch and the listener implemented on your watch will receive it and then, create and display the notification using the correct layout corresponding to the message sent, for example.
In this case, the best way is to implement a WearableListenerService on your wearable. Its lifecycle is handled by the system which binds it only when necessary.
To debug, you install the wear app on the wear emulator or your watch, you install the mobile app on your phone and finally you connect the two following this tutorial if you're using the emulator or via bluetooth if you're using a watch (link in the tutorial).
The Android Wear app connects your phone to your watch, whether it is an emulator or not. So, in order for your message to be received, you must have connected wear and mobile via Android Wear. Then, once the connection is established, you don't necessarily need to keep it open. So yes, the Android Wear app runs in background.

TTimer not working while iOS app in background

I have an app that needs to perform some actions based on a TTimer.
When the app becomes inactive (in background), the timer stops working.
I could not find any relevant options for UIBackgroundModes.
How can I make a timer keeps running?
iOS pauses your app automatically shortly after it goes into the background. You need to let iOS know that you are going to continue operating in the background.
In Project > Options > Version Info you can add to the array key UIBackgroundModes the string values for the services that require to continue running in the background. This gets added to the info.plist for your project on iOS.
You can consult Apple's documentation on what these values do . . .
Value - Description
audio - The app plays audible content in the
background.
location - The app provides location-based information to
the user and requires the use of the standard location services (as
opposed to the significant change location service) to implement this
feature.
voip - The app provides Voice-over-IP services. Apps with this
key are automatically launched after system boot so that the app can
reestablish VoIP services. Apps with this key are also allowed to
play background audio.
fetch - The app requires new content from the
network on a regular basis. When it is convenient to do so, the
system launches or resumes the app in the background and gives it a
small amount of time to download any new content. This value is
supported in iOS 7.0 and later.
remote-notification - The app uses
remote notifications as a signal that there is new content available
for download. When a remote notification arrives, the system launches
or resumes the app in the background and gives it a small amount of
time to download the new content. This value is supported in iOS 7.0
and later.
newsstand-content - The app processes content that was
recently downloaded in the background using the Newsstand Kit
framework, so that the content is ready when the user wants it. This
value is supported in iOS 5.0 and later.
external-accessory - The app
communicates with an accessory that delivers data at regular
intervals. This value is supported in iOS 5.0 and later.
bluetooth-central - The app uses the CoreBluetooth framework to
communicate with a Bluetooth accessory while in the background. This
value is supported in iOS 5.0 and later.
bluetooth-peripheral - The app
uses the CoreBluetooth framework to communicate in peripheral mode
with a Bluetooth accessory. The system will alert the user to the
potential privacy implications of apps with this key set. See Best
Practices for Maintaining User Privacy for more information on
privacy. This value is supported in iOS 6.0 and later.
Notice you need to actually select the mode that matches what you are doing. You can't just select fetch when really all you are doing is background processing.

Resources