skip to Main Content

I migrated my Cordova project to capacitor 6.0.0 (without a framework). My app uses jQuery Mobile, and the code is maintained in Android Studio. I managed to get all plugins to work except the cordova-plugin-fcm which is incompatible with capacitor. Instead I want to use the @capacitor-firebase/messaging, but I am unable to find any documentations/demos on how to include it in my project in Javascript.

The only documentation that I find is for capacitor with some type of framework.

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve it.

    1. Make sure the @capacitor-firebase/messaging plugin is installed

    2. Include this code in capacitor.config.json file inside "plugins"

       "FirebaseMessaging": {
               "presentationOptions": [
                   "badge",
                   "sound",
                   "alert"
               ]
           }
      
    3. Include this code in your JavaScript file.

       const { FirebaseMessaging } = Capacitor.Plugins.FirebaseMessaging;
      
       const checkPermissions = async () => {
           const result = await FirebaseMessaging.checkPermissions();
           return result.receive;
       };
      
       const requestPermissions = async () => {
           const result = await FirebaseMessaging.requestPermissions();
           return result.receive;
       };
      
       const getToken = async () => {
           const result = await FirebaseMessaging.getToken();
           return result.token;
       };
      
       const deleteToken = async () => {
           await FirebaseMessaging.deleteToken();
       };
      
       const getDeliveredNotifications = async () => {
           const result = await FirebaseMessaging.getDeliveredNotifications();
           return result.notifications;
       };
      
       const removeDeliveredNotifications = async () => {
           await FirebaseMessaging.removeDeliveredNotifications({
               ids: ['798dfhliblqew89pzads'],
           });
       };
      
       const removeAllDeliveredNotifications = async () => {
           await FirebaseMessaging.removeAllDeliveredNotifications();
       };
      
       const subscribeToTopic = async () => {
           await FirebaseMessaging.subscribeToTopic({ topic: 'news' });
       };
      
       const unsubscribeFromTopic = async () => {
           await FirebaseMessaging.unsubscribeFromTopic({ topic: 'news' });
       };
      
       const addTokenReceivedListener = async () => {
           await FirebaseMessaging.addListener('tokenReceived', event => {
           console.log('tokenReceived', { event });
           });
       };
      
       const addNotificationReceivedListener = async () => {
           await FirebaseMessaging.addListener('notificationReceived', event => {
           console.log('notificationReceived', { event });
           });
       };
      
       const addNotificationActionPerformedListener = async () => {
           await FirebaseMessaging.addListener('notificationActionPerformed', event => {
           console.log('notificationActionPerformed', { event });
           });
       };
      
       const removeAllListeners = async () => {
       await FirebaseMessaging.removeAllListeners();
       };
      
       //Display the token
       Capacitor.Plugins.FirebaseMessaging.getToken().then(
           function(value) { console.log(JSON.stringify(value)); },
           function(error) { console.log(JSON.stringify(error)); }
       );
      
       Capacitor.Plugins.FirebaseMessaging.addListener("messageReceived", (message) => console.log(message));
      
    4. To test app notification, first you need to get the token of your emulator or physical device. To do this, run the app, you will see the token in the console.

    5. Run your app. Make sure it's in the background (i.e., not visible on the screen). Then login to firebase console, choose send a message to a test device, enter the token and send the message.


  2. You just need to import it as described in the documentation:

    import { FirebaseMessaging } from '@capacitor-firebase/messaging';
    

    Here is an example of using a Capacitor plugin without any framework.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search