Invalidate Firebase Token on Logout (React Native Firebase) - firebase

What is the correct way to invalidate a firebase token on react-native-firebase when user logs out?
Firebase's deleteInstanceId isn't available in react-native-firebase.
In the Github issues, the following method was suggested: firebase.iid().delete()
However, after using this method once, I wasn't able to get the user to continue receiving notifications (using a newly generated firebase token) -- and the only way to start receiving firebase messages again, was to delete the app
This behavior happened on both iOS and Android.
The reason I want to invalidate the token upon logout -- is to make sure that this device will not receive notifications that are only intended for the user to see when logged-in

Related

Export Historical user data from Firebase

I have setup a firebase project to track users events and for supporting push notification in my IOS and Android apps. In my apps I was registering for notifications when user completes sign up. However I have a number of users who just opened the app and didn't complete registration. I am planning to send notifications to such users to give them a nudge.
However I don't have the notification tokens for such users in my database. So is there any option to export the details of such users from firebase where I can grab their notification tokens?
I tried using big query and audiences but that wont work with historical data.
It sounds like you're using FCM tokens to send messages. In that case you will always needs an FCM token to send a message to a specific app/device.
FCM doesn't know anything about users of your app; all it knows about are the tokens.
So if you want to send a message to an app instance where the user didn't sign in, you'll need to store the token from the app in your database as soon as the app starts - even before the user signs in. Then when the user signs in, you can associate the token with their UID.
With those steps out of the way, you can query for database for tokens without an associated UID and send them a reminder to complete the registration.

How to detect that firebase auth is now trying to sign in user?

When a page first loads on our React app / frontend, one of two cases is true:
Firebase auth found persisted creds and is trying to authenticate the user -- there will shortly be a callback on onAuthStateChanged
This isn't a registered user / no persisted creds found: Firebase auth is not trying to authenticate the user (they need to sign-in for the first time, or sign up), so there is not going to be a callback on onAuthStateChanged
We want to render different things in these two cases. To do this we need some way to know that Firebase Auth is actually attempting the sign-in.
How can we do this?
Simliar to Firebase Auth: How can I detect that firebase trying to auto signIn current user?
The only way of knowing if Auth is attempting to sign in is through your own knowledge of how the APIs are being invoked. There is no provided way to "spy" on the progress of the APIs. If you're not the one invoking the APIs (for example, using Firebase UI), then all you know is what an auth state observer tells you (fully signed in or signed out).
If you've just launched the app, and Firebase is trying to refresh the user token (from having previously been signed in), you don't really have a way of knowing how that's going. You have to wait for the first call to your observer to know if there is a user signed in or not. Your app should wait until that first callback in order to figure out how to proceed. The documentation doesn't really do a good job of explaining this.

FCM method to determine if token/user has disabled push Notifciations

I know there are methods to check both iOS and Android to see if the user has locally disabled push notifications for an app, that is done within the app itself. But if a user disables push notifications for an app is this reported back to FCM? Does FCM know not to send a notification to that user and if so, is there anyway of pulling that info out of FCM.
The reason for this question is if the user hasn't used the app in a while and they disabled push notifications - without the user starting the app (thus getting updated tokens/checking for permissions) there is no way to know the user disabled push notifications - unless we can extract that from FCM.
And along these same lines, if a user has uninstalled the app, does FCM get updated that the token is now invalid due to app removal and removes it from its db - and if so, is there anyway to way to extract this from FCM?
In both cases, we need to either update our DB that the user has disabled push notifications (and possibly use alternate communication methods) or mark the user as 'uninstalled' and thus remove that user from any app communication method (fcm, sms, email).

Enabling the notification feature for my firebase app

I have made an app with the use of firebase. It is basically a chat application in which you van send and receive the text and images. I want to add a functionality in it that whenever a user sends a msg, then another user should get a notification . When I try to send a notification through the firebase console, then it is working, but when a user messages through the app, then it is not showing any notification to another user. So, can anyone tell me that how can this functionality be achieved ?Also, provide some sample code to see how things are working
You need to use cloud functions for that:
https://firebase.google.com/docs/functions
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests
First, you can register the user to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("news");
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Then when the message is sent to the database, you can use onWrite() in cloud functions which is a database trigger to be able to send the notification.
https://firebase.google.com/docs/functions/database-events

Authentication with Firebase in React Native apps

I have a React Native App in which I use Firebase for database and authentication. I use custom authentication with Firebase where I generate a token on my server side code and pass on the token to the client and the client authenticates itself using signInWithCustomToken method. Earlier, we could specify an expiry period for the token and I could use the same token again and again. This was then changed in a later version to have an expiry period of one hour after being issued and it was mentioned that the Firebase SDK will make sure to maintain connections and the user wouldn't need to do the authentication again and again. I recently saw that in v3.6.2, there was some fixes to the authentication for react native
Fixed an issue that prevented user authentication states from
persisting when using Firebase Authentication with React Native
versions 0.37 and higher.
I am using the latest v3.6.6 at the moment. I still see that the user is authenticated every now and then when the app is foregrounded. I wanted to know how does authentication with firebase in react native apps work. The user will put the app in the background at which point it would be suspended by the phone OS or even killed in some conditions. When the user put the app in foreground again, should I expect that the Firebase SDK would make sure that the connection to firebase stays on or would it authenticate with firebase again?

Resources