Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

G.2 Init FCM

Hadi Tavakoli edited this page Sep 26, 2018 · 4 revisions

Initialize Firebase FCM

firebaseMessaging.ane can be initialized similar to other Firebase child ANEs. simply by calling the FCM.init(); method. However, considering that FCM is highly dependent on Google Play Services, you need to check for Google Play Services APK on Android (the iOS side doesn't need this check) before you init the FCM ANE. You can do so, like below after you initialized the firebaseCore.ane of course.

if (Firebase.os == Firebase.ANDROID) Firebase.checkGoogleAvailability(onCheckResult);
else onCheckResult(Firebase.SUCCESS);

function onCheckResult($result:int):void
{
	switch($result)
	{
		case Firebase.SUCCESS:
			
			trace("checkGoogleAvailability result = SUCCESS");
			
			// now you can use FCM
			initFCM();
			
		break;
		case Firebase.SERVICE_MISSING:
			
			trace("checkGoogleAvailability result = SERVICE_MISSING");
			
		break;
		case Firebase.SERVICE_VERSION_UPDATE_REQUIRED:
			
			trace("checkGoogleAvailability result = SERVICE_VERSION_UPDATE_REQUIRED");
			
		break;
		case Firebase.SERVICE_UPDATING:
			
			trace("checkGoogleAvailability result = SERVICE_UPDATING");
			
		break;
		case Firebase.SERVICE_DISABLED:
			
			trace("checkGoogleAvailability result = SERVICE_DISABLED");
			
		break;
		case Firebase.SERVICE_INVALID:
			
			trace("checkGoogleAvailability result = SERVICE_INVALID");
			
		break;
	}
}

After you made sure that Google Services are available, simply call FCM.init(); and the FCM service will start in your app. As soon as this method is called for the first time on the iOS side, a dialog will be presented to you asking for user permissions to send push notifications.

Access the device registration token

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token.

FCM.getInstanceId(onTokenReceived);

function onTokenReceived($token:String, $error:String):void
{
	if($error)
	{
		trace("onTokenReceived error: " + $error);
	}
				
	if($token)
	{
		trace("token: " + $token);
	}
}

The registration token may change when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.

To monitor the token generation, you need to listen to FcmEvents.TOKEN_REFRESH event. This event fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. Make sure you have added the required services/activities to your manifest.

FCM.listener.addEventListener(FcmEvents.TOKEN_REFRESH, onTokenRefresh);

function onTokenRefresh(e:FcmEvents):void
{
	// If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
	trace("onTokenRefresh = " + e.token);
}

Revoke the token

Incase you need to revoke the token or generate a new one, you need to have access to the FirebaseInstanceId API. Please read the information on this page to know how you can manage the Firebase iid API.

Introduction to Firebase ANEs collection for Adobe Air apps


Get Started with Firebase Core in AIR

  1. Prerequisites
  2. Add Firebase to your app
  3. Add the Firebase SDK
  4. Init Firebase Core
  5. Available ANEs
  6. Managing Firebase iid

Get Started with Analytics

  1. Add Analytics ANE
  2. Init Analytics ANE
  3. Log Events
  4. Set User Properties

Get Started with Crashlytics

  1. Add Crashlytics ANE
  2. Test Your Implementation
  3. Customize Crash Reports
  4. Upload .dSYM for iOS apps

Get Started with DynamicLinks

  1. Add DynamicLinks ANE
  2. Init DynamicLinks ANE
  3. Create DynamicLinks
  4. Receive DynamicLinks
  5. View Analytics

Get Started with Authentication

  1. Add Authentication
  2. Init Authentication
  3. Manage Users
  4. Phone Number
  5. Custom Auth
  6. Anonymous Auth
  7. State in Email Actions
  8. Email Link Authentication

Get Started with FCM + OneSignal

  1. Add FCM ANE
  2. Init FCM ANE
  3. Send Your 1st Message
  4. Send Msg to Topics
  5. Understanding FCM Messages
  6. init OneSignal

Get Started with Firestore

  1. Add Firestore
  2. Init Firestore
  3. Add Data
  4. Transactions & Batches
  5. Delete Data
  6. Manage the Console
  7. Get Data
  8. Get Realtime Updates
  9. Simple and Compound
  10. Order and Limit Data
  11. Paginate Data
  12. Manage Indexes
  13. Secure Data
  14. Offline Data
  15. Where to Go From Here

Get Started with Realtime Database

  1. Add Realtime Database
  2. Init Realtime Database
  3. Structure Your Database
  4. Save Data
  5. Retrieve Data
  6. Enable Offline Capabilities

Get Started with Remote Config

  1. Parameters and Conditions
  2. Add Remote Config
  3. Init Remote Config

Get Started with Performance

  1. Add Performance ANE
  2. Init & Start Monitoring

Get Started with Storage

  1. Add Storage ANE
  2. Init Storage ANE
  3. Upload Files to Storage
  4. Download Files to Air
  5. Use File Metadata
  6. Delete Files

Get Started with Functions

  1. Write & Deploy Functions
  2. Add Functions ANE
  3. Init Functions
Clone this wiki locally