Monaca Docs
Search…
Cordova Firebase Plugin
This Cordova plugin brings push notifications, analytics, event tracking, crash reporting and more from Google Firebase to your Cordova project.
The custom build debugger can not check push notifications. To check push notifications, please check with a debug build or a release build.

Demo

Preparation for iOS

When working with push notifications for iOS, an APNs authentication key or an APNs certificate is required. On this page, we will show you how to create a development APNs certificate for testing on an iOS debug build.
For a release build, a production APNs certificate is required.
In order to create a development APNs certificate, you should have signed up for the Apple Developer Program. Follow the instructions below to create the certificate:
    1.
    From the Apple Developer page, go to Account.
    2.
    Select Certificates, Identifiers & Profiles.
3. An App ID with push notifications enabled is required when creating an APNs certificate. Therefore, let’s create one. Under the Identifiers section, go to App IDs and click the
button in the upper right corner.
4. Fill in the following information:
    App Description: Input your app name here (e.g. Cordova Firebase Demo)
    Explicit ID: Select this option because a Wildcard App ID is not allowed for push notifications. Input a unique identifier for your app (e.g. io.monaca.firebase).
5. Scroll down to App Services section and tick the Push Notifications option. Then, click Continue.
6. You will be redirected to the Confirm your App ID page. Click Register to complete the process.
7. Now, you should be back on the iOS App IDs page. Select the App ID you’ve just created and click Edit.
8. Scroll down to the Push Notifications section and click Create Cerficate under Development SSL Certificate.
9. Click Continue.
10. After that, click Choose File to browse your CSR file. You can get this file from the Monaca Cloud IDE by going to Config → iOS Build Settings. Then, click Generate Key and CSR. After creating the CRS file, download it by clicking the Export button.
11. Next, click Continue again. When the certificate is ready, you can download it. Keep the file.
12. Upload the downloaded certificate by clicking the Upload Certificate button in the iOS Build Settings.
13. Click the Export icon of the target certificate displayed in Certificates registered in Monaca to export the p12 file. You will need it for the Firebase configuration in the upcoming section.

Configuring Firebase for push notifications

    2.
    Sign in with your Google account to enter the console.
    3.
    Click Add project.
4. Fill in the project information and click CREATE PROJECT. You will be redirected to the Project Overview page.
After creating a project, we can start configuring the push notification project for a specific platform.

For iOS

    1.
    Go to the Project settings.
2. Under the General section, click ADD APP and select iOS.
3. Enter your iOS Bundle ID (You can find it by going to Config → iOS App Settings). Then, click REGISTER APP.
4. Download the GoogleService-Info.plist file and place it in the root folder of your project. After that, click CONTINUE.
5. You will see instructions for adding the Firebase SDK into our project. However, we are not developing the app natively so we can skip this step. Just click CONTINUE to proceed. Then, click FINISH to complete the configuration. After that, you should see your iOS app on the Firebase overview page.
6. Now, it’s time to add the APNs certificates into this project. Go to Project settings and select CLOUD MESSAGING.
7. Scroll down to the iOS app configuration section and upload the APNs authentication key or the APNs certificates.
That's it! You are done with the iOS configuration.

For Android

    1.
    Go to the Project settings.
2. Under the General section, click ADD APP and select Android.
3. Enter your package name (You can find that by going to Config → Android App Settings). Then, click REGISTER APP.
4. Download google-services.json file and place it in the root folder of your project. After that, click CONTINUE.
5. You will see instructions for adding the Firebase SDK into our project. However, we are not developing the app natively so we can skip this step. Just click FINISH to complete the configuration. After that, you should see your Android app in the Firebase overview page.

Adding the plugin to the Monaca IDE

    1.
    From the Monaca IDE menu, go to Config → Manage Cordova Plugins.
    2.
    Click the Import Cordova Plugin button. Then, you can choose to import the plugin using a ZIP file or URL/package name.

Usage

After importing the plugin to the project, you can start using it by generating a token for your device and setting an event for when a user opens a notification. Please make sure to call the plugin API after the Cordova is loaded.
1
document.addEventListener("deviceready", function(){
2
window.FirebasePlugin.getToken(function(token) {
3
// save this server-side and use it to push notifications to this device
4
console.log(token);
5
}, function(error) {
6
console.error(error);
7
});
8
9
// Get notified when a token is refreshed
10
window.FirebasePlugin.onTokenRefresh(function(token) {
11
// save this server-side and use it to push notifications to this device
12
console.log("Refresh to get new token: " + token);
13
}, function(error) {
14
alert(error);
15
});
16
17
// Get notified when the user opens a notification
18
window.FirebasePlugin.onMessageReceived(function(message) {
19
console.log("Message type: " + message.messageType);
20
if(message.messageType === "notification"){
21
alert("Notification message received");
22
if(message.tap){
23
alert("Tapped in " + message.tap);
24
}
25
}
26
}, function(error) {
27
console.error(error);
28
});
29
}, false);
Copied!

API references

In this section, we only describe some main APIs used in our Demo. For a complete list of the API references, please refer to here.

getToken()

Get the device token. The token will be null if it is not established yet.
1
window.FirebasePlugin.getToken();
Copied!
Example
1
window.FirebasePlugin.getToken(function(token) {
2
// save this server-side and use it to push notifications to this device
3
console.log(token);
4
}, function(error) {
5
console.error(error);
6
});
Copied!

onTokenRefresh()

Get notified when a token is refreshed. This is the best way to get a valid token for a device as soon as the token is established.
1
window.FirebasePlugin.onTokenRefresh();
Copied!
Example
1
window.FirebasePlugin.onTokenRefresh(function(token) {
2
// save this server-side and use it to push notifications to this device
3
console.log(token);
4
}, function(error) {
5
console.error(error);
6
});
Copied!

onMessageReceived()

Get notified when a notification is opened.
1
window.FirebasePlugin.onMessageReceived();
Copied!
Example
1
window.FirebasePlugin.onMessageReceived(function(message) {
2
console.log("Message type: " + message.messageType);
3
if(message.messageType === "notification"){
4
alert("Notification message received");
5
if(message.tap){
6
alert("Tapped in " + message.tap);
7
}
8
}
9
}, function(error) {
10
console.error(error);
11
});
Copied!

hasPermission()

Check the permission to receive push notifications.
1
window.FirebasePlugin.hasPermission();
Copied!
Example
1
window.FirebasePlugin.hasPermission(function(hasPermission){
2
if (hasPermission)
3
alert("Permission to receive notification is granted.");
4
else
5
alert("Permission to receive notification is NOT granted.");
6
});
Copied!

grantPermission() (iOS only)

Grant permission to receive push notifications for iOS (will trigger a prompt if the permission has not been granted yet).
1
window.FirebasePlugin.grantPermission();
Copied!
Example
1
window.FirebasePlugin.grantPermission(function(){
2
alert("Permission is granted for iOS");
3
}, function(error){
4
alert(error);
5
});
Copied!

setBadgeNumber()

Set a number on the icon badge.
1
window.FirebasePlugin.setBadgeNumber(badgeNumber);
Copied!
Parameter
Name
Type
Description
badgeNumber
Number
The number on the icon badge
Example
After setting the badge number, close the app. Then, you will see the badge number appear on your app icon. If you want to remove the number, set it to 0.
1
window.FirebasePlugin.setBadgeNumber(5);
Copied!

getBadgeNumber()

Get the icon badge number.
1
window.FirebasePlugin.getBadgeNumber();
Copied!
Example
1
window.FirebasePlugin.getBadgeNumber(function(n) {
2
alert("Badge Number is " + n);
3
});
Copied!

unregister()

Unregister from Firebase to stop receiving push notifications. Call this when you logout user from your app.
1
window.FirebasePlugin.unregister();
Copied!
Example
1
window.FirebasePlugin.unregister(function(){
2
alert("Stop receiving push notifications.");
3
}, function(error){
4
alert(error);
5
});
Copied!

setScreenName()

Set the name of the current screen in the analytics.
1
window.FirebasePlugin.setScreenName(screenName);
Copied!
Parameter
Name
Type
Description
screenName
String
Screen name
Example
1
var page="FirebaseHome";
2
window.FirebasePlugin.setScreenName(page);
3
alert(page + " screen is tracked.");
Copied!

setUserId()

Set a user id for the analytics.
1
window.FirebasePlugin.setUserId(id);
Copied!
Parameter
Name
Type
Description
id
String
A unique identifier, associated with a particular user. Must be sent with each hit.
Example
1
//user ID for testing purpose
2
var myUserId="35009a79-1a05-49d7-b876-2b884d0f825b";
3
window.FirebasePlugin.setUserId("user_id");
4
alert("UserID is set to: " + myUserId);
Copied!
Last modified 2mo ago