• Stars
    star
    240
  • Rank 168,229 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 4 months ago

Reviews

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

Repository Details

Enable Firebase Cloud Messaging for Capacitor apps


Capacitor FCM

@capacitor-community/fcm

Capacitor community plugin for enabling FCM capabilities


Sponsors

Intenseloop

Maintainers

Maintainer GitHub Social
Stewan Silva stewones @stewones

Notice 🚀

We're starting fresh under an official org. If you were using the previous npm package capacitor-fcm, please update your package.json to @capacitor-community/fcm. Check out changelog for more info.

Installation

Using npm:

npm install @capacitor-community/fcm

Using yarn:

yarn add @capacitor-community/fcm

Sync native files:

npx cap sync

Notice

This plugin is intended to be used combined with Capacitor API for Push Notifications. Capacitor only provides APN token whereas this plugin offers the possibility to work with FCM tokens and more.

API

method info platform
subscribeTo subscribe to fcm topic ios/android
unsubscribeFrom unsubscribe from fcm topic ios/android
getToken get fcm token to eventually use from a server ios/android
refreshToken refresh fcm token to get a new one ios/android
deleteInstance remove local fcm instance completely ios/android
setAutoInit enable the auto initialization of the library ios/android
isAutoInitEnabled check whether auto initialization is enabled ios/android

Usage

import { FCM } from "@capacitor-community/fcm";
import { PushNotifications } from "@capacitor/push-notifications";

// external required step
// register for push
await PushNotifications.requestPermissions();
await PushNotifications.register();

// now you can subscribe to a specific topic
FCM.subscribeTo({ topic: "test" })
  .then((r) => alert(`subscribed to topic`))
  .catch((err) => console.log(err));

// Unsubscribe from a specific topic
FCM.unsubscribeFrom({ topic: "test" })
  .then(() => alert(`unsubscribed from topic`))
  .catch((err) => console.log(err));

// Get FCM token instead of the APN one returned by Capacitor
FCM.getToken()
  .then((r) => alert(`Token ${r.token}`))
  .catch((err) => console.log(err));

// Delete the old FCM token and get a new one
FCM.refreshToken()
  .then((r) => alert(`Token ${r.token}`))
  .catch((err) => console.log(err));

// Remove FCM instance
FCM.deleteInstance()
  .then(() => alert(`Token deleted`))
  .catch((err) => console.log(err));

// Enable the auto initialization of the library
FCM.setAutoInit({ enabled: true }).then(() => alert(`Auto init enabled`));

// Check the auto initialization status
FCM.isAutoInitEnabled().then((r) => {
  console.log("Auto init is " + (r.enabled ? "enabled" : "disabled"));
});

Add Google config files

Navigate to the project settings page for your app on Firebase.

iOS

Download the GoogleService-Info.plist file. In Xcode right-click on the yellow folder named "App" and select the Add files to "App".

Tip: if you drag and drop your file to this location, Xcode may not be able to find it.

Android

Download the google-services.json file and copy it to android/app/ directory of your capacitor project.

Certificate

iOS setup

  • Install homebrew (once)
  • brew install cocoapods (once a time)
  • ionic start my-cap-app --capacitor
  • cd my-cap-app
  • mkdir www && touch www/index.html
  • npx cap add ios
  • npm install --save @capacitor-community/fcm
  • npx cap sync ios (always do sync after a plugin install)
  • npx cap open ios
  • sign your app at xcode (general tab)
  • enable remote notification capabilities
  • add GoogleService-Info.plist to the app folder in xcode
// (optional) turn off `swizzling` in the `info.plist`
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>

Tip: every time you change a native code you may need to clean up the cache (Product > Clean build folder) and then run the app again.

Prevent auto initialization

If you need to implement opt-in behavior, you can disable the auto initialization of the library by following the Firebase docs.

Android setup

  • ionic start my-cap-app --capacitor
  • cd my-cap-app
  • mkdir www && touch www/index.html
  • npx cap add android
  • npm install --save @capacitor-community/fcm
  • npx cap sync android (always do sync after a plugin install)
  • npx cap open android
  • add google-services.json to your android/app folder

Now you should be set to go. Try to run your client using ionic cap run android --livereload.

Tip: every time you change a native code you may need to clean up the cache (Build > Clean Project | Build > Rebuild Project) and then run the app again.

Variables

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

  • $firebaseMessagingVersion version of com.google.firebase:firebase-messaging (default: 23.1.2)

Prevent auto initialization

If you need to implement opt-in behavior, you can disable the auto initialization of the library by following the Firebase docs.

Example

License

MIT

Contributors

Thanks goes to these wonderful people (emoji key):

Stew
Stew

💻 📖
Daniel Pereira
Daniel Pereira

💻 📖
Priyank Patel
Priyank Patel

💻
Nikolas
Nikolas

🚧
Ben Schattinger
Ben Schattinger

💻
James Manners
James Manners

💻
Borja Rodríguez
Borja Rodríguez

🚧
Karrlllis
Karrlllis

📖
jamesmah
jamesmah

💻
Josh Sharpe
Josh Sharpe

🚧
Mantas Šimkūnas
Mantas Šimkūnas

💻 📖
Olivier Overstraete
Olivier Overstraete

🚧
Hemang Kumar
Hemang Kumar

💻 🚧
Luca Ban
Luca Ban

📖
Alex Griffith
Alex Griffith

💻 🚧
bdirito
bdirito

🚧
Ryan
Ryan

🚧
Josué Moreno
Josué Moreno

💻
Marc
Marc

🚧
Florian Gyger
Florian Gyger

💻
jcesarmobile
jcesarmobile

💬 📖 🚧
Rami Khafagi
Rami Khafagi

💻
Bittor Poza
Bittor Poza

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

More Repositories

1

sqlite

Community plugin for native & electron SQLite databases
Swift
463
star
2

barcode-scanner

A fast and efficient (QR) barcode scanner for Capacitor
Java
435
star
3

electron

Deploy your Capacitor apps to Linux, Mac, and Windows desktops, with the Electron platform! 🖥️
TypeScript
327
star
4

bluetooth-le

Capacitor plugin for Bluetooth Low Energy
TypeScript
277
star
5

react-hooks

⚡️ React hooks for Capacitor ⚡️
TypeScript
244
star
6

generic-oauth2

Generic Capacitor OAuth 2 client plugin. Stop the war in Ukraine!
Java
232
star
7

admob

Community plugin for using Google AdMob
Java
209
star
8

http

Community plugin for native HTTP
Java
209
star
9

camera-preview

Capacitor plugin that allows camera interaction from HTML code
Java
188
star
10

stripe

Stripe Mobile SDK wrapper for Capacitor
Java
188
star
11

background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
Java
187
star
12

google-maps

Capacitor Plugin using native Google Maps SDK for Android and iOS.
Java
152
star
13

in-app-review

Let users rate your app using native review app dialog for both Android and iOS.
TypeScript
144
star
14

apple-sign-in

Sign in with Apple Support
Swift
137
star
15

vue-cli-plugin-capacitor

A Vue CLI 3/4 Plugin for Capacitor
JavaScript
131
star
16

keep-awake

⚡️ Capacitor plugin to prevent devices from dimming or locking the screen.
Java
125
star
17

firebase-analytics

Enable Firebase Analytics for Capacitor Apps
Java
122
star
18

contacts

Contacts Plugin for Capacitor
Java
114
star
19

tauri

Deploy your Capacitor apps to Linux, Mac, and Windows desktops, with the Tauri platform! 🖥️
TypeScript
108
star
20

native-audio

Java
104
star
21

facebook-login

Facebook Login support
Java
101
star
22

media

Capacitor plugin for saving and retrieving photos and videos, and managing photo albums.
TypeScript
100
star
23

text-to-speech

⚡️ Capacitor plugin for synthesizing speech from text.
Java
93
star
24

speech-recognition

Java
84
star
25

date-picker

Native DateTime Picker Plugin for Capacitor Apps
Swift
84
star
26

privacy-screen

⚡️ Capacitor plugin that protects your app from displaying a screenshot in Recents screen/App Switcher.
Swift
77
star
27

proposals

Plugin and platform requests ✋
74
star
28

app-icon

Capacitor plugin to programmatically change the app icon.
Java
74
star
29

firebase-crashlytics

⚡️ Capacitor plugin for Firebase Crashlytics.
Java
70
star
30

file-opener

Capacitor File Opener. The plugin is able to open a file given the mimeType and the file uri. This plugin is similar to cordova-plugin-file-opener2 without installation support.
Swift
64
star
31

intercom

Enable Intercom for Capacitor apps
TypeScript
57
star
32

photoviewer

PhotoViewer table images with fullscreen and sharing capabilities
Swift
49
star
33

examples

Examples of using Capacitor with popular web frameworks and libraries
JavaScript
46
star
34

safe-area

Capacitor Plugin that exposes the safe area insets from the native iOS/Android device to your web project.
Kotlin
46
star
35

welcome

Introduction to the Capacitor Community org 👋
37
star
36

appcenter-sdk-capacitor

Capacitor Plugin for Microsoft's Visual Studio App Center SDK.
TypeScript
35
star
37

in-app-purchases

WIP: In App Purchases plugin for Capacitor
Java
27
star
38

native-market

Java
26
star
39

realm

Java
25
star
40

firebase-remote-config

TypeScript
23
star
41

screen-brightness

Java
23
star
42

.github

Template repo for new community plugins
17
star
43

twitter

Capacitor plugin to enable TwitterKit
TypeScript
11
star
44

card-scanner

Simple card scanner for Capacitor Applications.
Swift
11
star
45

flipper

Java
10
star
46

auth0

TypeScript
9
star
47

volume-buttons

Capacitor Volume Buttons. The plugin enables to listen to hardware volume button presses. This plugin is based on https://github.com/thiagobrez/capacitor-volume-buttons
Swift
7
star
48

google-maps-examples

Vue
5
star
49

uxcam

UXCam and FullStory app analytics
Java
5
star
50

advertising-id

Allows access to the IDFA (iOS) and AAID (Android)
Swift
5
star
51

android-security-provider

Capacitor plugin with method to check and update the Android Security Provider.
Java
3
star
52

tap-jacking

Capacitor plugin to prevent tap jacking on Android devices
Java
2
star
53

mdm-appconfig

Capacitor community plugin for reading app configurations written by a MDM (see appconfig.org) such as VMWare Workspace One.
Java
2
star
54

exif

This plugin offers utility functions for interacting with image exif metadata
Swift
2
star
55

play-integrity

A Capacitor plugin to use the Play Integrity API
Java
1
star
56

device-check

A Capacitor plugin to use Apple's DeviceCheck API
Java
1
star