• Stars
    star
    162
  • Rank 232,284 (Top 5 %)
  • Language
    Java
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cordova plugin for Firebase Cloud Messaging

Cordova plugin for Firebase Cloud Messaging

NPM version NPM downloads NPM total downloads PayPal donate Twitter

Donate Your help is appreciated. Create a PR, submit a bug or just grab me 🍺

Index

Supported platforms

  • iOS
  • Android

Installation

$ cordova plugin add cordova-plugin-firebase-messaging

If you get an error about CocoaPods being unable to find compatible versions, run

$ pod repo update

Use variables IOS_FIREBASE_POD_VERSION and ANDROID_FIREBASE_BOM_VERSION to override dependency versions on Android:

$ cordova plugin add cordova-plugin-firebase-messaging \
    --variable IOS_FIREBASE_POD_VERSION="9.3.0" \
    --variable ANDROID_FIREBASE_BOM_VERSION="30.3.1"

Adding configuration files

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.

  1. Put google-services.json and/or GoogleService-Info.plist into the root directory of your Cordova project
  2. 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.

Set custom default notification icon (Android only)

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>

Set custom default notification color (Android only)

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>

Type Aliases

PushPayload

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).

Type declaration

Name Type Description
aps? Record<string, any> IOS payload, available when message arrives in both foreground and background.
data Record<string, any> Custom data sent from server
gcm? Record<string, any> Android payload, available ONLY when message arrives in foreground.
message_id string Message ID automatically generated by the server
sent_time number Time in milliseconds from the Epoch that the message was sent.

Functions

clearNotifications

clearNotifications(): Promise<void>

Clear all notifications from system notification bar.

Example

cordova.plugins.firebase.messaging.clearNotifications();

Returns

Promise<void>

Callback when operation is completed


deleteToken

deleteToken(): Promise<void>

Delete the Instance ID (Token) and the data associated with it.

Call getToken to generate a new one.

Example

cordova.plugins.firebase.messaging.deleteToken();

Returns

Promise<void>

Callback when operation is completed


getBadge

getBadge(): Promise<number>

Gets current badge number (if supported).

Example

cordova.plugins.firebase.messaging.getBadge().then(function(value) {
    console.log("Badge value: ", value);
});

Returns

Promise<number>

Promise fulfiled with the current badge value


getToken

getToken(format?): Promise<string>

Returns the current FCM token.

Example

cordova.plugins.firebase.messaging.getToken().then(function(token) {
    console.log("Got device token: ", token);
});

Parameters

Name Type Description
format? "apns-buffer" | "apns-string" Token representation (iOS only)

Returns

Promise<string>

Promise fulfiled with the current FCM token


onBackgroundMessage

onBackgroundMessage(callback, errorCallback?): void

Registers background push notification callback.

Example

cordova.plugins.firebase.messaging.onBackgroundMessage(function(payload) {
    console.log("New background FCM message: ", payload);
});

Parameters

Name Type Description
callback (payload: PushPayload) => void Callback function
errorCallback? (error: string) => void Error callback function

Returns

void


onMessage

onMessage(callback, errorCallback?): void

Registers foreground push notification callback.

Example

cordova.plugins.firebase.messaging.onMessage(function(payload) {
    console.log("New foreground FCM message: ", payload);
});

Parameters

Name Type Description
callback (payload: PushPayload) => void Callback function
errorCallback? (error: string) => void Error callback function

Returns

void


onTokenRefresh

onTokenRefresh(callback, errorCallback?): void

Registers callback to notify when FCM token is updated.

Use getToken to generate a new token.

Example

cordova.plugins.firebase.messaging.onTokenRefresh(function() {
    console.log("Device token updated");
});

Parameters

Name Type Description
callback () => void Callback function
errorCallback? (error: string) => void Error callback function

Returns

void


requestPermission

requestPermission(options): Promise<void>

Ask for permission to recieve push notifications (will trigger prompt on iOS).

Example

cordova.plugins.firebase.messaging.requestPermission({forceShow: false}).then(function() {
    console.log("Push messaging is allowed");
});

Parameters

Name Type Description
options Object Additional options.
options.forceShow boolean When value is true incoming notification is displayed even when app is in foreground.

Returns

Promise<void>

Filfiled promise when permission is granted.


setBadge

setBadge(badgeValue): Promise<void>

Sets current badge number (if supported).

Example

cordova.plugins.firebase.messaging.setBadge(value);

Parameters

Name Type Description
badgeValue number New badge value

Returns

Promise<void>

Callback when operation is completed


subscribe

subscribe(topic): Promise<void>

Subscribe to a FCM topic.

Example

cordova.plugins.firebase.messaging.subscribe("news");

Parameters

Name Type Description
topic string Topic name

Returns

Promise<void>

Callback when operation is completed


unsubscribe

unsubscribe(topic): Promise<void>

Unsubscribe from a FCM topic.

Example

cordova.plugins.firebase.messaging.unsubscribe("news");

Parameters

Name Type Description
topic string Topic name

Returns

Promise<void>

Callback when operation is completed

More Repositories

1

better-dom

Live extension playground
JavaScript
545
star
2

better-dateinput-polyfill

input[type=date] polyfill
JavaScript
259
star
3

cordova-plugin-firebase-analytics

Cordova plugin for Firebase Analytics
Java
150
star
4

cordova-plugin-firebase-authentication

Cordova plugin for Firebase Authentication
JavaScript
97
star
5

better-form-validation

Form validation polyfill for better-dom
JavaScript
60
star
6

better-details-polyfill

complete <details> polyfill using better-dom
JavaScript
57
star
7

cordova-plugin-firebase-dynamiclinks

Cordova plugin for Firebase Dynamic Links
Objective-C
55
star
8

cordova-support-google-services

DEPRECATED. Cordova plugin to add google services support
40
star
9

cordova-plugin-app-review

Cordova plugin to review app
Objective-C
38
star
10

better-ajaxify

A simple PJAX engine for websites
JavaScript
30
star
11

cordova-plugin-firebase-crash

Cordova plugin for Firebase Crashlytics
JavaScript
27
star
12

cordova-plugin-idfa

Cordova plugin to get Advertising ID (IDFA or AAID)
Objective-C
24
star
13

cordova-plugin-firebase-config

Cordova plugin for Firebase Remote Config
Objective-C
23
star
14

better-placeholder-polyfill

Placeholder attribute polyfill for better-dom
JavaScript
22
star
15

better-timeinput-polyfill

input[type=time] polyfill for better-dom
JavaScript
16
star
16

cordova-plugin-web-share

Web Share API polyfill for Cordova
Java
13
star
17

cordova-plugin-firebase-inappmessaging

Cordova plugin for Firebase In-App Messaging
JavaScript
10
star
18

better-emmet-plugin

Emmet abbreviation parser for better-dom
JavaScript
9
star
19

better-i18n-plugin

Internationalization plugin for better-dom
JavaScript
7
star
20

cordova-support-android-plugin

More convenient base CordovaPlugin class
Java
7
star
21

cordova-plugin-core-android-extensions

Useful methods for an android cordova app
Java
7
star
22

cordova-plugin-browser

Open browser activity in app
Java
7
star
23

formvalidation.js

Polyfill for form validation
JavaScript
6
star
24

better-elastic-textarea

Make textarea to expand on user input
JavaScript
6
star
25

better-time-element

Useful <time> element extensions
JavaScript
3
star
26

better-xhr

Better abstraction for XMLHttpRequest
JavaScript
2
star
27

cordova-plugin-material-snackbar

Cordova plugin for Material design Snackbars
Java
1
star
28

cordova-plugin-fastclick

Cordova plugin to remove click delays on browsers with touch UIs
JavaScript
1
star
29

better-popover-plugin

Popover plugin for better-dom
JavaScript
1
star
30

better-prettydate

Enhaces time element to update in realtime
JavaScript
1
star
31

cordova-plugin-alert

Display platform-specific modal dialogs in your cordova app
Objective-C
1
star