Cordova plugin for Firebase Cloud Messaging
![]() |
Your help is appreciated. Create a PR, submit a bug or just grab me 🍺 |
|---|
- iOS
- Android
$ cordova plugin add cordova-plugin-firebase-messaging
Use variables IOS_FIREBASE_SDK_VERSION and ANDROID_FIREBASE_BOM_VERSION to override SDK versions for different platforms:
$ cordova plugin add cordova-plugin-firebase-messaging \
--variable IOS_FIREBASE_SDK_VERSION="9.3.0" \
--variable ANDROID_FIREBASE_BOM_VERSION="30.3.1"
For cordova-ios below version 8: if you get an error about CocoaPods being unable to find compatible versions, run
$ pod repo update
Cordova supports resource-file tag for easy copying resources files. Firebase SDK requires google-services.json on Android and GoogleService-Info.plist on iOS platforms.
- Put
google-services.jsonand/orGoogleService-Info.plistinto the root directory of your Cordova project - Add new tag for Android platform
<platform name="android">
...
<resource-file src="google-services.json" target="app/google-services.json" />
</platform>
...
<platform name="ios">
...
<resource-file src="GoogleService-Info.plist" />
</platform>This way config files will be copied on cordova prepare step.
Setting a custom default icon allows you to specify what icon is used for notification messages if no icon is set in the notification payload. Also use the custom default icon to set the icon used by notification messages sent from the Firebase console. If no custom default icon is set and no icon is set in the notification payload, the application icon (rendered in white) is used.
<platform name="android">
...
<config-file parent="/manifest/application" target="app/src/main/AndroidManifest.xml">
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/my_custom_icon_id"/>
</config-file>
</platform>You can also define what color is used with your notification. Different android versions use this settings in different ways: Android < N use this as background color for the icon. Android >= N use this to color the icon and the app name.
<platform name="android">
...
<config-file parent="/manifest/application" target="app/src/main/AndroidManifest.xml">
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@drawable/my_custom_color"/>
</config-file>
</platform>PushPayload =
object
In general (for both platforms) you can only rely on custom data fields.
message_id and sent_time have google. prefix in property name (will be fixed).
clearNotifications():
Promise<void>
Clear all notifications from system notification bar.
Promise<void>
Callback when operation is completed
cordova.plugins.firebase.messaging.clearNotifications();deleteToken():
Promise<void>
Delete the Instance ID (Token) and the data associated with it.
Call getToken to generate a new one.
Promise<void>
Callback when operation is completed
cordova.plugins.firebase.messaging.deleteToken();getBadge():
Promise<number>
Gets current badge number (if supported).
Promise<number>
Promise fulfiled with the current badge value
cordova.plugins.firebase.messaging.getBadge().then(function(value) {
console.log("Badge value: ", value);
});getToken(
format?:"apns-buffer"|"apns-string"):Promise<string>
Returns the current FCM token.
| Parameter | Type | Description |
|---|---|---|
format? |
"apns-buffer" | "apns-string" |
Token representation (iOS only) |
Promise<string>
Promise fulfiled with the current FCM token
cordova.plugins.firebase.messaging.getToken().then(function(token) {
console.log("Got device token: ", token);
});onBackgroundMessage(
callback: (payload:PushPayload) =>void,errorCallback?: (error:string) =>void):void
Registers background push notification callback.
| Parameter | Type | Description |
|---|---|---|
callback |
(payload: PushPayload) => void |
Callback function |
errorCallback? |
(error: string) => void |
Error callback function |
void
cordova.plugins.firebase.messaging.onBackgroundMessage(function(payload) {
console.log("New background FCM message: ", payload);
});onMessage(
callback: (payload:PushPayload) =>void,errorCallback?: (error:string) =>void):void
Registers foreground push notification callback.
| Parameter | Type | Description |
|---|---|---|
callback |
(payload: PushPayload) => void |
Callback function |
errorCallback? |
(error: string) => void |
Error callback function |
void
cordova.plugins.firebase.messaging.onMessage(function(payload) {
console.log("New foreground FCM message: ", payload);
});onTokenRefresh(
callback: () =>void,errorCallback?: (error:string) =>void):void
Registers callback to notify when FCM token is updated.
Use getToken to generate a new token.
| Parameter | Type | Description |
|---|---|---|
callback |
() => void |
Callback function |
errorCallback? |
(error: string) => void |
Error callback function |
void
cordova.plugins.firebase.messaging.onTokenRefresh(function() {
console.log("Device token updated");
});requestPermission(
options:object):Promise<void>
Ask for permission to recieve push notifications (will trigger prompt on iOS).
| Parameter | Type | Description |
|---|---|---|
options |
{ forceShow: boolean; } |
Additional options. |
options.forceShow |
boolean |
When value is true incoming notification is displayed even when app is in foreground. |
Promise<void>
Filfiled promise when permission is granted.
cordova.plugins.firebase.messaging.requestPermission({forceShow: false}).then(function() {
console.log("Push messaging is allowed");
});setBadge(
badgeValue:number):Promise<void>
Sets current badge number (if supported).
| Parameter | Type | Description |
|---|---|---|
badgeValue |
number |
New badge value |
Promise<void>
Callback when operation is completed
cordova.plugins.firebase.messaging.setBadge(value);streamBackgroundMessage(
signal?:AbortSignal):AsyncGenerator<PushPayload,void,unknown>
Subscribes to the stream of incoming push notifications received while the app is in the background.
| Parameter | Type | Description |
|---|---|---|
signal? |
AbortSignal |
An optional signal to abort the stream. |
AsyncGenerator<PushPayload, void, unknown>
An async generator yielding message payloads.
const controller = new AbortController();
for await (const message of FirebaseMessaging.streamBackgroundMessage(controller.signal)) {
console.log("Received a background push notification:", message);
}streamMessage(
signal?:AbortSignal):AsyncGenerator<PushPayload,void,unknown>
Subscribes to the stream of incoming push notifications received while the app is in the foreground.
| Parameter | Type | Description |
|---|---|---|
signal? |
AbortSignal |
An optional signal to abort the stream. |
AsyncGenerator<PushPayload, void, unknown>
An async generator yielding message payloads.
const controller = new AbortController();
for await (const message of FirebaseMessaging.streamMessage(controller.signal)) {
console.log("Received a foreground push notification:", message);
}streamTokenRefresh(
signal?:AbortSignal):AsyncGenerator<string,void,unknown>
Subscribes to the FCM token refresh event stream.
| Parameter | Type | Description |
|---|---|---|
signal? |
AbortSignal |
An optional signal to abort the stream. |
AsyncGenerator<string, void, unknown>
An async generator yielding refreshed token strings.
const controller = new AbortController();
for await (const token of FirebaseMessaging.streamTokenRefresh(controller.signal)) {
console.log("New FCM Token arrived:", token);
}subscribe(
topic:string):Promise<void>
Subscribe to a FCM topic.
| Parameter | Type | Description |
|---|---|---|
topic |
string |
Topic name |
Promise<void>
Callback when operation is completed
cordova.plugins.firebase.messaging.subscribe("news");unsubscribe(
topic:string):Promise<void>
Unsubscribe from a FCM topic.
| Parameter | Type | Description |
|---|---|---|
topic |
string |
Topic name |
Promise<void>
Callback when operation is completed
cordova.plugins.firebase.messaging.unsubscribe("news");