How to get "Apps Instance Id" in Flutter - firebase

i want to setup firebase inapp messaging on my flutter apps.
first, it need FirebaseInstanceId as explained here, so i can test send message to my device.
However the document does not tell more detail how to get it on Android, especially on flutter.
any idea ?
Note : someone has claimed that he can use inapp-messaging in flutter, please see here
Thank you in Advance...

The Firebase Instance ID can be fetched using -instanceIDWithHandler on iOS or getInstanceId on Android.
At the time of writing, I am not aware of a standalone plugin that does this, meaning that you have two options:
Write your own plugin that wraps the native implementations of those methods
Use the firebase_messaging Flutter plugin that happens to expose the Instance ID via its getToken() method
If you go the firebase_messaging route (mind you, it means you're adding another dependency), you can do something like this:
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
and then, somewhere in your code:
var token = await _firebaseMessaging.getToken();
print("Instance ID: $token");
Update
When you call get token, you get something in the following format: NNNNNNN:MMMMMMMMMMMMMMMMMM. Take only the first part before the colon (NNNNNNNN) - this is the instance ID you need to test your In-App Messaging campaign.

You can run this in your terminal.
adb logcat | grep 'InAppMessaging'
02-26 23:47:44.835 28379 28379 I FIAM.Headless: Starting
InAppMessaging runtime with Installation ID dTEmOkEwRzC_xiO8YNPXs0

To retrieve the FirebaseInstanceId you need to implement the Firebase In-App messaging SDK and make sure your app connects to Firebase.
Once the app is connected to Firebase it will add the FirebaseInstanceId to the Android log once the App is run.
In the article you linked Google explains that you can find the FirebaseInstanceId by looking in the log for the string I/FIAM.Headless: Starting InAppMessaging runtime with Instance ID <YOUR_APP_ID>.
If you are using Android Studio you should be able to use the logcat window to browse the logs of the device (while debugging the App). The logcat window will also allow you to search and filter the logs so it should be relative easy to find the mentioned string.
So in short (in Android Studio):
Implement the Firebase In-App messaging SDK in your App;
Start debugging the App (preferably on a real Android device);
While debugging open the Logcat window;
Search the logs for the string I/FIAM.Headless: Starting InAppMessaging runtime with Instance ID;
The FirebaseInstanceId should be listed directly after the string.

Using flutterfire_installations, you can get:
Installation ID:
String id = await FirebaseAppInstallations.instance.getId();
Authentication token:
String token = await FirebaseAppInstallations.instance.getToken();

Related

How to deal with "auth/missing-identifier" error in Firebase?

I am trying to create a simple web app in node.js (backend only), which would store some user data on Firestore. I want to use my own credentials management, and I believe that signInWithCustomToken() should be my ticket to ensure different users can't access each other's data. The problem however is that if I run
const firebaseAuth = getAuth(app)
// authToken is a jwt token created by my backent
const response = await signInWithCustomToken(firebaseAuth, authToken)
console.log(response)
I keep getting the mysterious auth/missing-identifier error, about which I couldn't find anything (it is not identical to auth/missing-client-identifier I believe).
Where is the problem? Is my code wrong, or did I set up something incorrectly in the Firebase?
Using firebase 9.8.1
I was also getting this error using firebase v9, but I was only getting this error with Facebook login. I switched to use firebase/compat methods and it worked for me. It's a temporary fix until more light can be shed on this error. I couldn't find the auth/missing-identifier error in any firebase docs so I'm not entirely sure what the underlying issue actually is.

Firebase remote config alternatives

Are there some alternatives to Firebase Remote Config?
I need to deliver an App for chinese market and I'm not sure that it will work
I've solved with Parse https://parseplatform.org/
there's also a baas implementation provided by https://www.back4app.com/ with a free tier
Yeah, you can use Huawei Remote Configuration, it mostly matches Firebase for API style.
Sign up for the Huawei console https://developer.huawei.com/consumer/en/agconnect/remote-configuration/
Add the dependencies:
implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300'
implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.5.2.300'
implementation 'com.huawei.hms:hianalytics:6.0.0.300'
Fetch the same as Firebase Remote Config:
config = AGConnectConfig.getInstance()
config.fetch(0) // a value of 0 here is for DEBUGGING ONLY, delete for prod (giving a 12 hour refresh period)
.addOnSuccessListener {
config.apply(it)
Log.d(TAG, "Applied")
//update based on RemoteConfig
}
Reference: https://blog.blundellapps.co.uk/remote-configuration-using-appgallery-connect/

React Native - Jest testing with firebase

I am a newbie in Firebase. I have read that it is good to unit test as you go. So this is what I have been trying to do lately. I currently have a problem when trying to test render on a class that uses Firebase. This is the following code I have been trying to fix:
import 'react-native';
import React from 'react';
import MainTab from '../../components/MainTab';
import Enzyme from 'enzyme';
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const tree = renderer.create(
<MainTab/>
).toJSON();
expect(tree).toMatchSnapshot();
});
However, I am getting the following error on the test when trying to obtain the current user's id from firebase:
Has anyone stumbled into this error before? P.S. Don't go hard on me if this is something really vacuous, just trying to learn.
1. signInWithEmailAndPassword
signInWithEmailAndPassword(email, password) returns
firebase.Promise containing non-null firebase.User
Asynchronously signs in using an email and password.
This method will be deprecated and will be updated to resolve with a
firebase.auth.UserCredential as is returned in
firebase.auth.Auth#signInAndRetrieveDataWithEmailAndPassword.
For now, the implementation for this method is in below:
firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
// user signed in
// - user.displayName
// - user.email
// - user.uid // <---- the user's unique ID
})
You can see firebase.User properties here:
https://firebase.google.com/docs/reference/js/firebase.User
You can read more details about this question here https://stackoverflow.com/a/39959002/3332734
2. For your case...
If you need to test the email/password authentication, I suggest you to use a firebase "dev" project and testing with user credentials for this dev firebase project.
Be careful, don't store your UID for other tests!
Firebase dont't provides a test for authentication yet.
3. Firebase beta to 1.0 version
Easier unit testing using a new firebase-functions-test npm module that simplifies writing unit tests.
See more about it in:
Launching Cloud Functions for Firebase v1.0
Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0
I hope I have been useful!

Can Firebase RemoteConfig be accessed from cloud functions

I'm using Firebase as a simple game-server and have some settings that are relevant for both client and backend and would like to keep them in RemoteConfig for consistency, but not sure if I can access it from my cloud functions in a simple way (I don't consider going through the REST interface a "simple" way)
As far as I can tell there is no mention of it in the docs, so I guess it's not possible, but does anyone know for sure?
firebaser here
There is a public REST API that allows you to read and set Firebase Remote Config conditions. This API requires that you have full administrative access to the Firebase project, so must only be used on a trusted environment (such as your development machine, a server you control or Cloud Functions).
There is no public API to get Firebase Remote Config settings from a client environment at the moment. Sorry I don't have better news.
This is probably only included in newer versions of firebase (8th or 9th and above if I'm not mistaken).
// We first need to import remoteConfig function.
import { remoteConfig } from firebase-admin
// Then in your cloud function we use it to fetch our remote config values.
const remoteConfigTemplate = await remoteConfig().getTemplate().catch(e => {
// Your error handling if fetching fails...
}
// Next it is just matter of extracting the values, which is kinda convoluted,
// let's say you want to extract `game_version` field from remote config:
const gameVersion = remoteConfigTemplate.parameters.game_version.defaultValue.value
So parameters are always followed by the name of the field that you defined in Firebase console's remote config, in this example game_version.
It's a mouthful (or typeful) but that's how you get it.
Also note that if value is stored as JSON string, you will need to parse it before usage, commonly: JSON.parse(gameVersion).
Similar process is outlined in Firebase docs.

NotificationHub Push Notification returns : The Token obtained from the Token Provider is wrong

I have Wp8.1 Silverlight app that receives push notification (WNS) from Mobileservice (the old azure service).
I therefore wanted to update to the new service because of the new features. I have now created/upgraded a new server to use App Service - Mobile App. And tested push notification with the sample app from azure (everything works).
Going back to my app WP8.1 -> Adding the new package Microsoft.Azure.Mobile.Client through NuGet (2.0.1), there is the issue that the Microsoft.WindowsAzure.Mobile.Ext does not contain the 'GetPush' extension. It seems like it is missing it? looking to the WP8 version, it only registers to MPNS, and I need WNS. So I do not know if any other assembly could be used.
Can I add another assembly reference?
Update
The following code lets me register the device on the server, and I can see the device register correctly. where the channelUri and the installationInformation are retrieved by the client and send to the server.
Installation ins = new Installation();
ins.Platform = NotificationPlatform.Wns;
ins.PushChannel = uTagAndChan.ChannelUri;
ins.Tags = uTagAndChan.Tags;
ins.InstallationId = uTagAndChan.installationInformation;
await hubClient.CreateOrUpdateInstallationAsync(ins);
Sending a test toast-notification to the registered tags, results in the following error :
The Token obtained from the Token Provider is wrong
Searching on this issue I found Windows Store App Push Notifications via Azure Service Bus. Which the proposed solution says to register to the notification hub directly from the app, I would rather not have the app to have directly access to the hub. But is this the only way? (mind you the answer was not accepted, but I will try it all though it is not a desired solution)
Update
Registering for notifications via client (WP8.1 Silverligt), makes a registration to MPNS, which I do not want.
The snippet on the server registers a WNS, the two registrations can be seen here:
The URI retrieval is done using
var channel = await Windows.Networking.PushNotifications.PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
which in the description states it returns a WNS. This seems to infer that the registration I am doing on the server (code snippet in the top) is correct and the registration on the client is faulty.
But the registration on the image seems wrong. Shouldn't the PNS Identifier be different for the two registrations? also expiration date seems wrong ?
How to mend this since the GetPush() (which was available in the sample registered the client correctly for notifications) does not exist in the NuGet package?
Update
I read one place that deleting and recreating the NotificationHub could help. I will try this today. Even IF it works, it would be more desirable to have the solution, and to know if the registrations are done correctly?
Temporary solution:
Deltede, recreated, inserted Package SID and Secret. And it works again (strange)!
Still interested in the underlying issue!
Deleted and recreated the service, setting all the same settings made it work again.
I had same issue with my UWP. But in my case I had issue with self signed certificate.
When I set the AppxPackageSigningEnabled property to True (in .csproj) then notifications stopped working and I got "The token obtained from the Token Provider is wrong" (Test send from Azure Portal).
The certificate must have same issuer as Publisher in Identity element in .appxmanifest file.

Resources