Android: How to handle user logout Firebase Cloud Messaging in 2021 - android

So, I just want to handle the situation when user logs out from the app.
I call method FirebaseMessaging.getInstance().deleteToken() when user logs out, and it works as expected, i.e. user doesn't receive push messages anymore...
But if there is no internet connection method deleteToken() doesn't help. User still gets push messages when a connection is established despite the fact that he logged out.
How can I handle this?

Firebase Cloud Messaging has no knowledge of the user, which explains why it can't change whether it sends/displays a message based on authentication status.
The main problem here is that notification messages are displayed by the system if the app is not being used, and you can't control what the system does with them based on authentication state.
The best way to deal with these situations is to only send data messages (without a notification property). The display of such data-only messages is handled by your application code, so your application code can check whether a user is signed-in, and decide whether to display the message based on that.
I also recommend reading up on FCM messages types to learn more about the distinction between these types.

Related

Can I set the "Difference push receive setting in firebase?"

I am now trying to make a mobile app push notification service on Firebase. But the function description is not quite specific as I expected.
I want to know these functions are available on Firebase. If not, It would be pleasure If you tell me other tool.
User can select the push message types they want to receive.(Ex. Receive sale information push, Do not receive game event push)
Instantly send auto push messages when user triggered certain condition.(Ex. Send appreciate push message when user closed their first app-open)
Thank you
Can not tell in much description here:
1. User can select the push message types they want to receive.(Ex.
Receive sale information push, Do not receive game event push)
You can use FCM's channeling feature,
You can set different channels.
Show the list of channels to user.
User can subscribe to the required channel.
User will receive the specific channel notification only.
Please refer : Notification Channel
2. Instantly send auto push messages when user triggered certain condition.(Ex. Send appreciate push message when user closed their first app-open)
You have manage this thing in your front end and backend logic.
Like on app close send request to the backend (can use onDestroy method )server and then the backend server will send notification.
Hope this will help you.
For Feature 1:
You can achieve this without even doing anything. Just send all notifications to everyone. However, use different Notification Channels. Users, can then choose to turn on/off certain notification channels using the Android system features (in relatively newer versions of android).
Another way could be to send these notifications to different FCM Topics. Give the users a settings pages, where there can select what kinds of notifications they would like to receive. In response to their selections, subscribe or unsubscribe them to the respective FCM Topic.
For Feature 2:
There could be several hundred ways of doing this. Can provide better advice if you could provide more information about your requirement.
If you talk about your example requirement
(Send appreciate push message when user closed their first app-open)
You can do it without any server, or push messaging scheme. Just keep track of the first_open event inside the app using Shared Preferences. Once you detect a first_open event, just compose a notification inside of the app locally, and show it whenever you like.

Can i disable push notifications via Firebase?

I am implementing push notification in my app after having it for IOS for a while.
Use case: I have user login, and accounts stored on our servers. I only want to send push notifications when a user is logged in, and only for the user currently logged in. Notifications are targeted to the individual user.
To do this, i fetch the token using
FirebaseInstanceId.getInstance().getToken()
, send push token to our server when user logs in, and remove it on logout.
Everything works as far as registering token, sending the token etc., HOWEVER, there could be scenarios where this doesn't work, for example if a user logs out with flight mode, so our server still has the token and thinks it should still send them.
On IOS, there are two local functions, register/unregisterforremotenotifications, that basically turns notifications on/off, regardless of whether my server could be contacted. I can call these on login/logout, and IOS won't show any remote notifications for my app, and i'm safe.
However, with Firebase, i can send the token to the server on login -
for logout, however, its more complex since there's no "local" system-function to call that i can find.
The best thing i've figured out, is to always send a 'Data' notification,
as described in this question,
so that my notification service always gets called, even in the background, and there check if i am logged in, and not show the notification if i'm not.
However, the notification for the wrong user will still be sent to the phone, and it's a risk, for example if i, god forbid would have a bug... or the notification gets logged somewhere in the system.
Sooo, my question is if there's any way to disable notifications on logout via Firebase?
I hope this makes sense, thoughts much appreciated!
Yes, what you do is you create your own Login and Logout APIs.
In your Login you should be storing your Token for PUSH notifications.
then whatever data triggers need to PUSH do a loop and build a push for known registered tokens.
On logout, simply delete that token from your Database and the loop will no longer include it for PUSH. You are correct using DATA tag will only work in foreground, but could be for wrong user that is correct.
If you do not have a backend, then please provide more clarity as to where/how you are storing and using your tokens to PUSH so I can help you further.
I have done this exact scenario to avoid wrong person getting push, but I support foreground and background on my scenario, so only logout or expired token will disable the PUSH on my app.
There are two ways:-
Your app supports multi user login at the same time scenarios :-
You can probably store the device token in share preferences with a boolean flag , when user relaunch the app , check if the flag for deletion is set then you can try deregistering from the service on server.
If it fails then again you can do the same , so it would be something like checking boolean flag for account deletion on every app launch to make sure it deregister.
Your app supports single usr:-
You can simply delete firebase instance Id before signing into the account. This would take care of the scenario where a different user had signed out offline and you were not able to de register from your service.
You can also handle scenarios when to show notification based on accountId or user Id of the signed in user.

When to register for topics using Firebase Notifications?

I'm using Firebase's FCM for push notifications for an app. There is a need to subscribe to certain topics from the moment the user starts the app.
So far I've handled "topics" and various metrics on our own server, but as we're moving logic parts to their appropriate place to lessen the load on our server, this needs to be moved to Firebase itself.
And we want to handle things a bit differently. Users will be able to subscribe and unsubscribe to/from certain notification groups, however FCM's documentation does not mention which is the time, which callback is the proper place to handle this.
The flow would be the following:
User installs app
User launches app
App updates cached data (information that changes in larger intervals, e.g. every 3-6 months)
App pulls synced data, including topics list, from Google account
App registers device for push notifications
App subscribes user to "all" topic (all users that want to receive generic notifications are registered here. Unsubscribing is only possible if the user unchecks the "send me notifications" option in settings)
App subscribes to the topics synced in step 4
What isn't clear is WHERE to place steps 6 and 7. Do I put it into my implementation of FirebaseInstanceIdService, into OnTokenRefresh on Android, and in Messaging.SharedInstance.Connect or InstanceId.Notifications.ObserveTokenRefresh on iOS? Do I need to re-register to topics when an FCM token change happens?
The app is written in Xamarin, so we're using FCM for both iOS and Android.
Where you place the code for subscribing the token depends entirely up to you. Usually though, it is placed on the initial activity of the app. This ensures that the user will be subscribed to that certain topic.
However in your use-case, you could make a method that checks if the Notification Settings for your app is ticked or not, if yes, subscribe the token, if not, unsubscribe. Then simply call this method on your initial activity.
With regard to "Do I need to re-register to topics when an FCM token change happens?", you don't have to. Referring to this answer by #DiegoGiorgini:
If the token is "refreshed" the topic subscriptions are maintained.
The register to topic should be placed in the View (Activity class in Android). It depends on where you need it. It could be achieved by using this line of code
FirebaseMessaging.Instance.SubscribeToTopic("promotion");
Note that when you subscribes to topic that does not exist, the topic will be created in the server. However the creation requires long time so the topic could not appear instantly in your Firebase console.
Later on, when you want to unsubscribe (probably after logout), just call this inside your View as well.
FirebaseMessaging.Instance.UnsubscribeFromTopic("promotion");
Hope this could help.

FCM notifications sent to topics not received anymore

My Android client app does not receive any Firebase push notifications targeting topics, however I immediately receive notifications sent to all app users or notifications sent to specific devices.
I didn't change anything in my code and I checked whether the client is correctly subscribed to topics.
For further details about my subscription logic:
In order to make it easy for my web service to send notifications to a specific user, each user is subscribed to a topic entitled with his user-id whenever he logs in from the client app.
Is this approach weak somehow? Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
Should I otherwise register the device token to my database every time it's updated? And then send the notification to that specific token?
It is highly suggested that developers save the generated registration token for each device for later use. As mentioned in the docs:
After you've obtained the token, you can send it to your app server and store it using your preferred method.
In your case, it is preferable. It'll remove the added action of subscribing the device to a topic. Plus it can be useful to track the message status using Diagnostics tool should you need it in the future.

Data exchange between two users using Firebase

I am not sure how is this process called. Say I am a user of the app and I want to know if there is a message for me (or a status change that I need to know about), I am not sure if this is the best way but I am trying to do it like this:
Firebase Structure
Users > User A > Status = "No messages"
Each user has a node Status as you can see above.
When user A sends a message to user B, user A changes user B's Status node.
User B, that had been listening to his own Status node, is now aware that there is something new and can go read the message.
Is this possible and safe making use of Firebase only?
On a simpler note, and more often used scenario similar to yours is like a Chat Application. User A sends a message to User B, User B then receives a notification from Firebase.
From what you have described, you intend to have a listener in place for the Status node, and inform the user whenever it changes. This seems okay, but from what was advised to me before, keeping the listener active tends to have a corresponding active socket on Android, which adds to battery consumption.
What I suggest you make use of is firebase-cloud-messaging:
Firebase Cloud Messaging is a component of the Firebase suite of tools for cross-platform application development.
Send unlimited upstream/downstream messages
Send messages to individual devices or a user segment
Handle all aspects of queueing and delivery
Optimize for battery efficiency
Using FCM, you can notify a client app that new email or other data is available to sync. You can send notifications to drive user reengagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.
I think this Firebase Codelab for a Chat App might also be helpful.

Categories

Resources