Swift 4 & Firebase: onDisconnectRemoveValue fired when app go background - swift

One of my company's app's requirements is a tracking system, where a user can track other nearby users when using our app. So there is a map and user's location (coordinates) will be updating to Firebase so other users can see on their map.
The most important thing is that we don't want sign out/offline users' location data on Firebase (because we want users only to be able to see online nearby users) so I use Firebase's onDisconnectRemoveValue() method to automatically remove disconnected users' data. The method worked great. BUT, if the app goes to the background (or screen is turned off), onDisconnectRemoveValue is fired too, so the data will be deleted (I don't want it to be deleted).
I searched the Internet, found this SO post.
This method is designed that way, I think I cannot do anything about it.
So my question is: If I want to remove a data on Firebase if the user is disconnected (loss internet connection OR app closed/killed), how can I do that?
Additional info: because I want users can updating their location to Firebase even if the App go to the background or device screen is off so I cannot check the connection at the client (can check, but when connection loss detected, cannot send remove data request to Firebase).

Related

How to detect Realm app user has been deleted?

app.currentUser.delete deletes the Realm app user from the server side. However, it seems on other logged-in devices, and the data upload is still working with no auth error.
On other logged-in devices, re-launching the app would detect that the user has been deleted. I also tried to use isLoggedIn but for authenticated devices, it seems always stays “true”.
RealmApp.shared.currentUser?.isLoggedIn
How to detect Realm app user has been deleted on other existing logged-in devices?
User presence is a challenge with Realm; it doesn’t have a baked in user presence system.
While you can monitor when a user is Online or Offline by setting a flag or var when they log in or log out (for example) - there’s no way to monitor in-between or unexpected changes of presence or deletion.
For example, a user logs in - the app can set a var (synched with the server) to ‘Online’ that other devices are observing so their UI can be updated. Whenever that flag changes, other devices or users of the app will know about it.
But what if the app crashes, the user d/c’s for whatever reason or the user force-quits the app. That var is now “Stuck” to the on position and other devices would never know they were offline.
For this use case, you can add an observer to a object in Realm that stores user status and sync's with the server. If the user logs out on one of the devices they are logged into for example, the other devices could be notified of that change. If the user is deleted, a "is deleted" flag could be set which notifies other devices or users of that change.
Expanding on that a bit - Deleting a user could trigger a server side function to then followup with perhaps removing all of their data, cleaning up references or notifying other devices of that event.

Flutter Notifications Panel

I'm using with success the flutter local notification plugin. Now I'm struggling for create a page with all notifications.
Is there a way to collect all incoming local notifications of my app (even if dismissed and/or not clicked) in a list (the classic notification page like FB etc.)
I only notice that I can track the tapped notification but not only the arrival notification.
Thanks!
This unfortunately is not possible, because notifications are not stored anywhere. They exist in memory on the device, until the user dismisses them, or they are replaced with a message of a similar ID, or the OS just removes them altogether.
Your best bet, is to generate a table on your mobile backend, that will store these notifications (the same ones that will be broadcasted to the users notification center), then get the app to read directly from this table, and store it on a local SQLite database.
These notifications that are broadcasted, will need to be sent from the backend itself, with the exact same content, as that being stored on the table I mentioned. This will ensure data integrity between the notification center, and that of the app.
[EDIT]
Please make sure that you use FCM (Firebase Cloud Messaging) for mobile push notifications. These are completely free, as per the documentation:
https://firebase.google.com/docs/cloud-messaging
For Tutorials, please look at these:
https://medium.com/#jun.chenying/flutter-tutorial-part3-push-notification-with-firebase-cloud-messaging-fcm-2fbdd84d3a5e
https://www.freecodecamp.org/news/how-to-add-push-notifications-to-flutter-app/

Custom Notification Depending on action

SWIFT 3 / Firebase
app like instagram/Facebook
Notifications
I am working on letting users know when another user e.g. liked their post, made a friend request etc.
I set up everything for cloud messaging with firebase and can send bulk messages at all my devices.
Now I want to introduce notifications which contain the current user, the user who did the action and what happened.
Unfortunately I did not find anything about this despite the general implementing of firebase cloud messaging.
My questions are:
1: Do i have to structure a database (e.g. .child("notifications").child(userId).... ?
2: How do I get the token from the stranger user I address with the notification?
3: How can I make the input variable?
I am happy about all kinds of help!

How to handle multiple push notifications with user data arrived at different times?

My app is receiving APNs sent from server to Apple backend. Naturally a user may not open the app once a notification arrives to user's device. In meantime my server may push more notifications. They all contain some user data that is important when a notification is processed. So how to deal with it? iOS won't bundle and give me a batch, will it?
Here are ways how I am going to tackle it, none of which is simple.
Server keeps track of not seen data and upon arriving a new request always sends a batch of all new notifications, reflecting the count as badge count.
Client is opened by taping on notification popup. In this case it has all needed data in didReceiveRemoteNotification.
OR
Client ignores notification popup and opens app (possibly later) by tapping on app icon. In this case didReceiveRemoteNotification is not called and thus app has to fetch all needed data from server.
OR
Server never sends any user data and client always checks for new stuff every time it starts or fetches data in didReceiveRemoteNotification.
Anything else? Something simpler I am missing?
Number 4 is the right approach. There is no guarantee that any of your app code will run when an APN is received, except on iOS7. So when your app starts, it has to check with your servers for any new information that it should display.
It's simplest to code this to alway ask your servers for the latest information to display, rather than rely on the information in the APN. Use the information in the APN only to determine which new information to navigate to, so that the app displays whatever the user tapped on.
This has changed with iOS7, where you can use the remote-notification background mode to be launched whenever a push message arrives. See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:

Call status change web-service when my App is deleted

In my application , i works with web-services and when user log in into my app my app is sending a request with status 1 means loged in and when on log out button click sending a request with status 0.
Now problem is , when user removes app from devices , status in my server is remain 1(log in) , hence other user can see him available while his app is not in device. so is there any way by which i can send request when my app removes from device (i don't think it is possible) or is there any other way that i can do in my backend side ?
Thanks in advance.
It is not possible to call a web service when the user deleting the app from an ios device. There are three methods to came to know that whether your application is there in user's ios device. But there are few limitations also.
Activate Push notification: By doing this, device will get registered with Apple's push notification service. When user delete the app from device, the registration will be revoked from APNS server and through the APNS feedback service, you can get to know whether the application is existing. (Limitation: If the user did not agree with receiving push notifications, then the app will not be registered with APNS and you never came to know that whether application is existing or not)
Activate Location Based service: If your application enables location based service, then your application will get periodic location updates in a location delegate method. In this delegate, you can call a webservice and keep update the status of user even the application is in background. (Limitation: If user disables the location update, then your server will not get info about user status)
Periodically Call a Webservice From you app: This is possible only if your app is active. (Limitation: When you application pushed in to background, your application will be in suspended mode, so it will not possible to call webservice)
Sorry Unfortunately Apple not provide any method that user Uninstall app from user's device, There is no such method.
When user delete any application device does give the alert "Do you want to delete this application" with option "Delete" and "Cancel". You don't have to write any specific code for this.
I just assume that There is one method in which you can find out when user is about to delete your app. That is you need to implement push notification Apple server will inform you when you try to push to an uninstalled instance. Other than that there's no way to detect it but i am not sure its helpful or not.
You can't do this from within the app. You would want to do something like have a periodic task which runs on the backend, checking the last activity date of logged in users and setting them to 'not available' after some configured period of inactivity. This will probably require some changes to the backend to record last activity date and a change to the app so that while it's open it sends a periodic 'heartbeat' to the backend. You probably want to make the timeout quite big (say 15 minutes, big enough to not have a large impact on performance).