Home

Awesome

Capacitor Firebase Push

Maintenance npm

👉🏼 Note: this Plugin is developed for Capacitor V3

This Plugin it used for Firebase Push Messages. It does support Data (silent) notifications!

<!-- DONATE -->

Donate

This and other Open-Source Cordova Plugins are developed in my free time. To help ensure this plugin is kept updated, new features are added and bugfixes are implemented quickly, please donate a couple of dollars (or a little more if you can stretch) as this will help me to afford to dedicate time to its maintenance. Please consider donating if you're using this plugin in an app that makes you money, if you're being paid to make the app, if you're asking for new features or priority bug fixes.

<!-- END DONATE --> <!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

Table of Content

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Install

npm install capacitor-firebase-push
npx cap sync

Shutout

❤️

This Plugin was created to match every requirement I had for my app. These Plugins helped my a lot to create this one:

Setup

Android

Just follow the Android Setup Guide here

Variables

This plugin will use the following project variables (defined in your app's variables.gradle file):

Push Notifications icon

On Android, the Push Notifications icon with the appropriate name should be added to the AndroidManifest.xml file:

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/push_icon_name" />

If no icon is specified Android will use the application icon, but push icon should be white pixels on a transparent backdrop. As the application icon is not usually like that, it will show a white square or circle. So it's recommended to provide the separate icon for Push Notifications.

Android Studio has an icon generator you can use to create your Push Notifications icon.

iOS

Start setting up your iOS Project like described here: https://capacitorjs.com/docs/v3/guides/push-notifications-firebase#integrating-firebase-with-our-native-ios-app

Stop before the Add the Firebase SDK via CocoaPods Part - we don't need that one.

After you updated the project (npx cap update ios), you have to add the following Initialization Code to your AppDelegate.swift:

  1. Add the Firebase Import:

    import Firebase
    
  2. Add the following two functions:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
         Messaging.messaging().apnsToken = deviceToken
         Messaging.messaging().token(completion: { (token, error) in
             if let error = error {
                 NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
             } else if let token = token {
                 NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token)
             }
         })
     }
    
     func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
         NotificationCenter.default.post(name: Notification.Name.init("didReceiveRemoteNotification"), object: completionHandler, userInfo: userInfo)
     }
    

Config

Push notification appearance in foreground

On iOS you can configure the way the push notifications are displayed when the app is in foreground by providing the `presentationOptions in your capacitor.config.ts as an Array of Strings you can combine.

Possible values are:

An empty Array can be provided if none of the previous options are desired. pushNotificationReceived event will still be fired with the push notification information.

plugins: {
  FirebasePush: {
    presentationOptions: ["badge", "sound", "alert"]
  }
}

These fields can be overwritten if you pass create_notification: true in the data Part of the notification.

API

<docgen-index> </docgen-index> <docgen-api> <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

checkPermissions()

checkPermissions() => Promise<PermissionStatus>

Check permission to receive push notifications.

Will always return "granted" on Android

Returns: <code>Promise<<a href="#permissionstatus">PermissionStatus</a>></code>

Since: 1.0.0


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

Request permission to receive push notifications.

Will always return "granted" on Android

Returns: <code>Promise<<a href="#permissionstatus">PermissionStatus</a>></code>

Since: 1.0.0


register()

register() => Promise<void>

Register the app to receive push notifications.

Since: 1.0.0


unregister()

unregister() => Promise<void>

Should be called to unregister the Firebase Instance. For example if a User logs out.

Since: 1.1.0


getBadgeNumber()

getBadgeNumber() => Promise<BadgeCount>

Get icon badge Value

Only available on iOS

Returns: <code>Promise<<a href="#badgecount">BadgeCount</a>></code>

Since: 1.2.0


setBadgeNumber(...)

setBadgeNumber(options: BadgeCount) => Promise<void>

Set icon badge Value

Only available on iOS

ParamType
options<code><a href="#badgecount">BadgeCount</a></code>

Since: 1.2.0


getDeliveredNotifications()

getDeliveredNotifications() => Promise<NotificationsResult>

Get notifications in Notification Center

Returns: <code>Promise<<a href="#notificationsresult">NotificationsResult</a>></code>

Since: 1.2.0


removeDeliveredNotifications(...)

removeDeliveredNotifications(options: NotificationsIds) => Promise<void>

Remove notifications from the notifications screen based on the id

ParamType
options<code><a href="#notificationsids">NotificationsIds</a></code>

Since: 1.2.0


removeAllDeliveredNotifications()

removeAllDeliveredNotifications() => Promise<void>

Remove all notifications from the notifications screen

Since: 1.2.0


addListener('token', ...)

addListener(eventName: 'token', listenerFunc: (result: TokenResult) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

Called when a new fcm token is created

ParamType
eventName<code>'token'</code>
listenerFunc<code>(result: <a href="#tokenresult">TokenResult</a>) => void</code>

Returns: <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>> & <a href="#pluginlistenerhandle">PluginListenerHandle</a></code>

Since: 1.0.0


addListener('message', ...)

addListener(eventName: 'message', listenerFunc: (message: any) => void) => Promise<PluginListenerHandle> & PluginListenerHandle

Called when a new message is received

ParamType
eventName<code>'message'</code>
listenerFunc<code>(message: any) => void</code>

Returns: <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>> & <a href="#pluginlistenerhandle">PluginListenerHandle</a></code>

Since: 1.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

Remove all native listeners for this plugin.

Since: 1.0.0


Interfaces

PermissionStatus

PropTypeSince
receive<code><a href="#permissionstate">PermissionState</a></code>1.0.0

BadgeCount

PropTypeSince
count<code>number</code>1.2.0

NotificationsResult

PropTypeSince
notifications<code>any[]</code>1.2.0

NotificationsIds

PropTypeSince
ids<code>string[]</code>1.2.0

PluginListenerHandle

PropType
remove<code>() => Promise<void></code>

TokenResult

PropType
token<code>string</code>

Type Aliases

PermissionState

<code>'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'</code>

</docgen-api>

Changelog

The full Changelog is available here