Firebase sometimes get token NULL for ios devices - firebase

I'm using Ionic 3 + Cordova to develop an app. In the app, I've imported the Firebase Cloud Message library to send push notifications to android/ios users. The problem is that I've already set the certifications on Apple developer panel but sometimes the token returned by the function "get_token()" on my app is returning null only for IOS users and only sometimes. Sometimes this function returns the correctly token and then the push notifications works correctly, but sometimes not.
I've already tried remove all the certifications on the developer apple menu.
The get_token() function is in the app.component.ts file, what means that the token is generated when the user open the app.
Getting the token by the firebase server:
try {
const fcmToken = await this.fcm.getToken();
Settings.push_token = fcmToken;
}
Importing the library:
import { FCM } from '#ionic-native/fcm';
This FCM variable is passed to the fcm variable inside the constructor:
public fcm: FCM
I'm hoping that the problem is on the firebase tool to get the token, because I didn't find any solution on internet.

I had the same issue and resolved it by calling get_token() only after platform is ready, when fcm is already connected:
this.platform.ready().then(() => { // first, make sure platform is ready
this.fcm.getToken().then(token => { // save the token in the backend

Related

Do i need to be concerned with firebase refresh tokens with regard to sending cloud messages?

UPDATE:
Upon successful client authentication with Firebase
iOS hybrid ionic Client receives an FCM token via the Firebase SDK and sends it to my cloud function which stores the client token into a document in a user collection in firestore
I have another cloud function that is responsible for sending out push notifications to individual users via their FCM tokens respectively, by retrieving the token from that same Firestore collection.
I have tested this to be working correctly, albeit without a refresh token, just the initial token.
My question is, do i need to be subscribing to the FCM refresh token client side and updating that user document in my Firestore collection with the refresh token to ensure the firebase cloud push notification method is always retrieving a token that has not yet expired? (see method for retrieving refresh token referenced below)
Or could i be retrieving the valid token from within the user object which is reachable via a cloud function server side with the firebase-admin SDK method admin.auth().getUser(uid)?
To be clear, the client is a hybrid ionic/ios application and i am retrieving the token client side via the ionic native firebase method Firebase.getToken() which works in conjunction with the cordova-plugin-firebase plugin:
import { Firebase } from '#ionic-native/firebase/ngx';
constructor(private firebase: Firebase) { }
...
this.firebase.getToken()
.then(token => console.log(`The token is ${token}`)) // save the token server-side and use it to push notifications to this device
.catch(error => console.error('Error getting token', error));
this.firebase.onNotificationOpen()
.subscribe(data => console.log(`User opened a notification ${data}`));
this.firebase.onTokenRefresh()
.subscribe((token: string) => console.log(`Got a new token ${token}`));
Reference link --> #ionic-native/firebase
The refresh token provided by Firebase Authentication is not going to be helpful for sending messages with Firebase Cloud Messaging. What you will need to do is associate the device token provided by FCM to the UID of the signed in user. Since users might be using multiple devices, it's customary to allow for multiple tokens to be associated with one UID.
The Firebase Admin SDK will not be helpful in getting tokens. The token must be provided by the client.

React native with firebase (push) notifications

So I have registration/login handled on my side using Laravel. I tried adding React-native-firebase so after login I could subscribe to topic like notifications/:userId so I would get notifications only for that user as currently every user is getting push notifications.
I can't find any guide on how to make this so it will work when app is in "dead" state or minimized.
I'd appreciate any help.
Thanks
You can use the below mentioned library for firebase integration.
https://rnfirebase.io/
Firebase has the Firebase Cloud Messenger, which has some notification control and badges from notifications built into it. I hope that helps, here is a link React-Native (https://rnfirebase.io/docs/v3.1.x/messaging/reference/messaging) and Firebase Docs (https://firebase.google.com/docs/reference/js/firebase.messaging)
You have generate fcmtoken after user log in, and save it to database.
After that from server side you have to fetch the fcmtoken for that particular user and call firebase pushnotification api / service.
Notification Payload :
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
If you want to send message/notification to multiple devices minor change is
tokens: ["token1", "token2"]

Parameters to the Firebase Push Messaging System

I am using Firebase Push notification its works fine but i have a question what if,
I need the ability to provide parameters to the Firebase push
messaging system that will allow me to display a message that, when
clicked, goes to a specified web link - in a web view. Most
importantly, this needs to function when the user does not have the
app loaded.
Is it possible ?? I gone through the documentation of firebase but didn't get anything about it.
For sending notifications to a user when they might not have the app running, use Firebase Cloud Messaging.
For sending messages to a user when they might not have the app installed, use Firebase Dynamic Links or Invites.
May be you have to try "Advance options" under the pushnotification by sending the key value pair like following(link is parameter and you set this parameter value in firebase console):
window.FirebasePlugin.onNotificationOpen ((notification) {
if notification.link != '' && notification.tap == true
ref = cordova.InAppBrowser.open(notification.link, '_blank', 'location=yes')
window.open = cordova.InAppBrowser.open
}

firebase cloud messaging with react native?

I have a react native project that is already connected to my firebase project. I use firebase as my database. Everything works great. But...
I am trying to get a simple firebase cloud messaging test to work with my react native android app. As I said, I already have firebase in use with my app for db purposes and for authentication.
I am using the react-native-firebase package (http://invertase.io/react-native-firebase) to do all my firebase work. I followed the directions on their site to get the messaging to work, but I haven't had any success.
I have followed the installation directions here: http://invertase.io/react-native-firebase/#/installation-android
All the database stuff works fine, but no luck with messaging. I am sending out test notifications using the firebase notifications console. I get the firebase token using this code in my app.
firebase.messaging().getToken()
.then((token) => {
firebase.database().ref(user.providerData[0].uid+'/agents/'+user.providerData[0].uid+'/fcmToken').set(token);
});
That code successfully gets the token and saves it to my firebase database. I then copy the token manually from my db and use it in my firebase notification console to send out a test notification. I know that the app is supposed to be running in the background. To get my app in the background I simply hit the home button. Then I send my test message. I don't get any errors in the firebase notification console and it says my token I supplied is all good. After a few seconds it says the notification status is completed. However in my app, nothing appears in my system tray, no notifications.
Does my app have to be in production mode or something? Anyone else get react-native-firebase to work with firebase cloud messaging on android?
I use the firebase console to send test messages

Firebase Cloud Messaging set up but not receiving push notifications from Firebase console

I've been trying to debug Firebase push notifications for quite a long time now, and got no luck. I believe I have set up provisional profiles and APNs certificate correctly. When I do not include the method
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .unknown)}
App receives notifications in foreground when sent from Firebase Notification console because it gets printed out, but it does not receive notification in background.
If I add the above method to it, it does not receive notification in foreground and background.
Anyone knows what is wrong?
I have already enabled push notifications, remote notifications, and I have setup the APNs certificate correctly. I have also tried reinstalling the app but it does not work. I have also tried all the stackoverflow posts already regarding this issue.
The code snippet that you've provided is for manual mapping of APNS token with the device registration token. Basically, you need to explicitly add this when the swizzling is disabled for your app.
Make sure to use the right token type: FIRInstanceIDAPNSTokenTypeSandbox for the sandbox environment, or FIRInstanceIDAPNSTokenTypeProd for the production environment. If you don't set the correct type, messages are not delivered to your app.
In addition, make sure that the priority is set to High in the Advanced options section when creating a new message in the Firebase Notification console.
If you're using a data message type to send notification to the client apps via FCM, then it is an intended behavior.
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
Per the Firebase documentation:
On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection.

Resources