Capacitor plugin to enable features from Firebase Cloud Messaging
This plugin is intended to be used together with the capacitor api for Push Notifications.
- subscribeTo
- unsubscribeFrom
- getToken
- deleteInstance
android only
import { FCM } from "capacitor-fcm";
const fcm = new FCM();
import { Plugins } from "@capacitor/core";
const { PushNotifications } = Plugins;
//
// Subscribe to a specific topic
PushNotifications.register()
.then(_ => {
fcm
.subscribeTo({ topic: "test" })
.then(r => alert(`subscribed to topic`))
.catch(err => console.log(err));
})
.catch(err => alert(JSON.stringify(err)));
//
// Unsubscribe from a specific topic
fcm
.unsubscribeFrom({ topic: "test" })
.then(r => alert(`unsubscribed from topic`))
.catch(err => console.log(err));
}
//
// get remote token
fcm
.getToken()
.then(r => alert(`Token ${r.token}`))
.catch(err => console.log(err));
}Navigate to the project settings page for your app on Firebase.
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.
Download the google-services.json file and copy it to android/app/ directory of your capacitor project.
ionic start my-cap-app --capacitorcd my-cap-appnpm install --save capacitor-fcmmkdir www && touch www/index.htmlsudo gem install cocoapodsnpx cap add iosnpx cap sync iosnpx cap open ios- sign your app at xcode (general tab)
- enable remote notification capabilities
- add
GoogleService-Info.plistto the app folder in xcode - turn
swizzlingoff on app'sinfo.plist
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
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.
ionic start my-cap-app --capacitorcd my-cap-appnpm install --save capacitor-fcmmkdir www && touch www/index.htmlnpx cap add androidnpx cap sync androidnpx cap open android- add
google-services.jsonto yourandroid/appfolder [extra step]in android case we need to tell Capacitor to initialise the plugin:
on your
MainActivity.javafile addimport io.stewan.capacitor.fcm.FCMPlugin;and then inside the init callbackadd(FCMPlugin.class);
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.
(coming soon)
Cheers 🍻
Follow me @Twitter
MIT