I have multiple sender id in project. As per Firebase Document, below is the code for Firebase initialization:
FirebaseOptions.Builder builder = new FirebaseOptions.Builder()
.setApplicationId("ApplicationId")
.setApiKey("your_api_key")
.setDatabaseUrl("https://your-app.firebaseio.com")
.setStorageBucket("your-app.appspot.com");
FirebaseApp.initializeApp(this, builder.build());
Below is the reference:
Use multiple projects in your application
App is receiving push notification from multiple Firebase project/ multiple sender Id. Can I use default initialization (using google-services.json) or initialization would be done manually as per the mentioned code/link?
Related
I have an application that is different for each user, id application is different for each user, so idApplication is dynamic. My problem is that json retrieved from firebase is linked to a single application id. How can I use one file json for all the applications I generate dynamically?
_______édit ____
I don't know thé application id, this application id is modified on a filé jenkins.properties on my project Android, and when jenkins générate New apk it do it with thé New id
Instead of relying on google-services.json and associated gradle plugin you can dynamically create FirebaseApp using something like following (I have this in dagger module)
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setApiKey(apiKey)
.setApplicationId(someApplicationId)
.setDatabaseUrl(databaseUrl)
.setStorageBucket(storageBucket)
.build();
firebaseApp = FirebaseApp.initializeApp(context, firebaseOptions, "MyApp");
I have implemented Fcm in my App and thats working great.. In My app i am creating notification groups by
$data = array();
$data["operation"] = "create";
$data["notification_key_name"] = "gcm";
$data["registration_ids"] = array("token");
and iam successfully received my notificationkey for frequent use... and if the token changes to another should i remove the tokenid from the notification group and add the new one everytime ?? or fcm will take care of it???
Yes. It's the developer's responsibility to manage these changes in the Device Group Messaging (see my answer here).
You'll have overwrite/replace the old token with the new one.
I am aware of the ability to create a named FirebaseApp instance with custom options for use with, for example, FirebaseDatabase, but I specifically need the ability to use FirebaseMessagaging against different (determined at runtime) Firebase projects.
The problem I have is that FirebaseMessaging.getInstance() doesn't support the name argument.
Is this in any way possible?
The reason I need to support separate projects is that our client connects to different customers server, so each customer will have their own firebase account and be generating their own notifications.
If it is not possible, is there any way to isolate one customer from another, so they can't possibly send notifications to another customer's device (assuming they could obtain a valid target device token in some way)?
The Firebase Blog contains a post about this exact issue, and provides a guide for how to handle it.
The blog post describes two steps:
Disable FirebaseInitProvider
Add this to the AndroidManifest.xml to disable the default initialization code:
<provider
android:name="com.google.firebase.provider.FirebaseInitProvider"
android:authorities="${applicationId}.firebaseinitprovider"
tools:node="remove"
/>
Call FirebaseApp.initializeApp(Context, FirebaseOptions)
As soon as you have the necessary configuration ready, initialize Firebase on your own with:
FirebaseOptions.Builder builder = new FirebaseOptions.Builder()
.setApplicationId("1:0123456789012:android:0123456789abcdef")
.setApiKey("your_api_key")
.setDatabaseUrl("https://your-app.firebaseio.com")
.setStorageBucket("your-app.appspot.com");
FirebaseApp.initializeApp(this, builder.build());
The Google Services Plugin documentation describes how to get the values you need from the JSON:
{YOUR_CLIENT} is the client object that contains a client_info/android_client_info/package_name that matches your package name (application id).
google_app_id: {YOUR_CLIENT}/client_info/mobilesdk_app_id
gcm_defaultSenderId: project_info/project_number
default_web_client_id: {YOUR_CLIENT}/oauth_client/client_id (client_type == 3)
ga_trackingId: {YOUR_CLIENT}/services/analytics-service/analytics_property/tracking_id
firebase_database_url: project_info/firebase_url
google_api_key: {YOUR_CLIENT}/api_key/current_key
google_crash_reporting_api_key: {YOUR_CLIENT}/api_key/current_key
To allow a single client to receive messages from multiple senders you can use the FirebaseInstanceId.getToken(A_SENDER_ID, "FCM") method. So there is no need to have retrieve multiple Messaging instances, to manage multiple senders.
Once the message arrives you can filter on the from field of the RemoteMessage object that is passed via the onMessageReceived callback.
If you navigate to FirebaseMessaging class, you'll see there's a package private getInstance which takes a firebaseApp. Also you'll see getInstance() is also using that with the default firebase app as the parameter. You can get it by reflection.
val firebaseApp = buildFirebaseApp()
val method = FirebaseMessaging::class.java.getDeclaredMethod("getInstance", FirebaseApp::class.java)
// Set the non-public accessible if security allowed
method.isAccessible = true
val fcmMessaging = method.invoke(null, firebaseApp) as FirebaseMessaging
When creating Firebase invite intent I try to add link to iOS app as described in documentation:
intent = new AppInviteInvitation.IntentBuilder(context.getString(R.string.invitation_title))
.setMessage(context.getString(R.string.invitation_message))
.setOtherPlatformsTargetApplication(
AppInviteInvitation.IntentBuilder.PlatformMode.PROJECT_PLATFORM_IOS,
"1059710961")
.build();
"1059710961" and "mobi.appintheair.wifi" both cause the same error:
AppInviteAgent: Create invitations failed due to error code: 3
AppInviteAgent: Target client ID must include non-empty and valid client ID: 1059710961. (APPINVITE_CLIENT_ID_ERROR)
What is the correct format for this parameter?
To get this client ID you have to do the following:
Register you iOS app in the Firebase Console
Download GoogleServices-Info.plist for iOS app as we download google-services.json for Android
Look in it and found value for the key CLIENT_ID (will be something like this 123456789012-abababababababababababababababab.apps.googleusercontent.com)
Add it to builder:
intent = new AppInviteInvitation.IntentBuilder(context.getString(R.string.invitation_title))
............
.setOtherPlatformsTargetApplication(
AppInviteInvitation.IntentBuilder.PlatformMode.PROJECT_PLATFORM_IOS,
"123456789012-abababababababababababababababab.apps.googleusercontent.com")
.build();
the client_id is the one in the plist you download from the firebase console for your iOS app
Below is the how to from google to register for GCM. Where in the second line of code do I put my appid? Comme to think about it is the appid the name of my project on the Google App Engine?
sender is the ID of the account authorized to send messages to the application, typically the email address of an account set up by the application's developer.
app is the application's ID, set with a PendingIntent to allow the registration service to extract application information.
For example:
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", "someemail#oo.com");
startService(registrationIntent);
The code in your quote is partly wrong. The value of sender used to be an email address (in the now deprecated C2DM), but in GCM the sender should contain the Google API Project ID.
sender is the project number of the account authorized to send messages to the Android application.
app is the Android application's ID, set with a PendingIntent to allow the registration service to extract Android application information.
According to http://developer.android.com/google/gcm/gcm.html
app is the Android application's ID, set with a PendingIntent to allow
the registration service to extract Android application information.
GCM extracts your app ID from the pending intent you are passing. So your second line is as it should be.
But I recommend you to use the gcm.jar library instead. It makes handling GCM much easier:
Add gcm.jar in Eclipse IDE (Google Cloud Messaging)