How to get "Apps Instance Id" in Flutter
Asked Answered
S

4

11

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...

Shererd answered 26/6, 2019 at 7:56 Comment(2)
Two great videos, Raja and FireshipLeenaleeper
I found the instance Id on Android using logcat. Easiest solutionTiffaneytiffani
L
21

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.

Loyceloyd answered 7/8, 2019 at 6:27 Comment(5)
i try to input instance_id / token that i got from _firebaseMessaging.getToken(), but i got error in firebase console : Invalid format. Please check the docs for retrieving the instance ID. Any Idea ?Shererd
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.Loyceloyd
As per October/2019 it's necessary to pass full token, INCLUDING content after colonPriscillaprise
Where is it actually documented that the first part of the token is in fact the stable instance id?Aunt
You are a life saver. For some reason, using the official method is not showing the instance Id in console. This method works. I wonder why it is not documented thoughChiachiack
P
6

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

Poplin answered 26/2, 2021 at 18:26 Comment(0)
M
1

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):

  1. Implement the Firebase In-App messaging SDK in your App;
  2. Start debugging the App (preferably on a real Android device);
  3. While debugging open the Logcat window;
  4. Search the logs for the string I/FIAM.Headless: Starting InAppMessaging runtime with Instance ID;
  5. The FirebaseInstanceId should be listed directly after the string.
Mayhew answered 26/6, 2019 at 8:43 Comment(2)
I'm using VSCODE, where will I find this?Crocidolite
Not sure if it will show up in the "DEBUG CONSOLE" window. Otherwise you can always fallback on the shell command as mentioned by @prisar: adb logcat | grep 'InAppMessaging' or use the code example mentioned by @david-airapetyan.Mayhew
B
0

Using flutterfire_installations, you can get:

  • Installation ID:

    String id = await FirebaseAppInstallations.instance.getId(); 
    
  • Authentication token:

    String token = await FirebaseAppInstallations.instance.getToken(); 
    
Barbarous answered 11/1, 2022 at 7:46 Comment(1)
old method, does not work anymoreValet

© 2022 - 2024 — McMap. All rights reserved.