-
Notifications
You must be signed in to change notification settings - Fork 16
G.2 Init 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.
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);
}
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.
Enjoy building Air apps – With ♥ from MyFlashLabs Team
Introduction to Firebase ANEs collection for Adobe Air apps
Get Started with Firebase Core in AIR
- Prerequisites
- Add Firebase to your app
- Add the Firebase SDK
- Init Firebase Core
- Available ANEs
- Managing Firebase iid
Get Started with Authentication
- Add Authentication
- Init Authentication
- Manage Users
- Phone Number
- Custom Auth
- Anonymous Auth
- State in Email Actions
- Email Link Authentication
Get Started with FCM + OneSignal
- Add FCM ANE
- Init FCM ANE
- Send Your 1st Message
- Send Msg to Topics
- Understanding FCM Messages
- init OneSignal
- Add Firestore
- Init Firestore
- Add Data
- Transactions & Batches
- Delete Data
- Manage the Console
- Get Data
- Get Realtime Updates
- Simple and Compound
- Order and Limit Data
- Paginate Data
- Manage Indexes
- Secure Data
- Offline Data
- Where to Go From Here
Get Started with Realtime Database
- Add Realtime Database
- Init Realtime Database
- Structure Your Database
- Save Data
- Retrieve Data
- Enable Offline Capabilities
Get Started with Remote Config
- Add Storage ANE
- Init Storage ANE
- Upload Files to Storage
- Download Files to Air
- Use File Metadata
- Delete Files