<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marco Improda</title>
    <description>The latest articles on DEV Community by Marco Improda (@marcoimproda90).</description>
    <link>https://dev.to/marcoimproda90</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F612325%2F88231099-8097-4fa2-966b-a790893934fa.jpg</url>
      <title>DEV Community: Marco Improda</title>
      <link>https://dev.to/marcoimproda90</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marcoimproda90"/>
    <language>en</language>
    <item>
      <title>How to set up Firebase Push Notifications with Ionic &amp; Vue.js</title>
      <dc:creator>Marco Improda</dc:creator>
      <pubDate>Mon, 10 May 2021 10:31:25 +0000</pubDate>
      <link>https://dev.to/marcoimproda90/how-to-configure-firebase-push-notifications-with-ionic-vue-js-38n7</link>
      <guid>https://dev.to/marcoimproda90/how-to-configure-firebase-push-notifications-with-ionic-vue-js-38n7</guid>
      <description>&lt;p&gt;Did you have issues configuring Push Notifications with Ionic &amp;amp; Vue.js? Did you not found the documentation for Vue, but only for Angular? &lt;em&gt;This is something I had too&lt;/em&gt; and that's why I have decided to make this post. By the end, you have your Ionic application ready and working with Firebase's Cloud Messaging service.&lt;br&gt;
Plus, you will found a repository with a basic app that has already everything up to work.&lt;/p&gt;

&lt;p&gt;So let's begin!&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 1 - Create our app
&lt;/h1&gt;

&lt;p&gt;First of all, make sure to create a new Ionic Vue.js app with &lt;a href="https://ionicframework.com/docs/cli" rel="noopener noreferrer"&gt;Ionic CLI&lt;/a&gt;. Open your Terminal or CMD/PowerShell and write&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ionic start --type=vue&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Set all the requirements for your application and open the folder of your project with your favourite code editor.&lt;/p&gt;

&lt;p&gt;Open the file &lt;strong&gt;capacitor.config.json&lt;/strong&gt; and set an &lt;strong&gt;appId&lt;/strong&gt;. Could be for example: &lt;strong&gt;com.myionicapp.demo&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcim8zyz7ugdd5k66klr5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcim8zyz7ugdd5k66klr5.png" alt="appId"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now go on &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase&lt;/a&gt;, select Get Started &amp;amp; Add Project.&lt;/p&gt;

&lt;p&gt;On Android package name set the appId that you had set previous on your &lt;strong&gt;capacitor.config.json&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fds609d1kp96fir8k1x5n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fds609d1kp96fir8k1x5n.png" alt="Firebase appId"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save &lt;strong&gt;google-services.json&lt;/strong&gt;. We need it later to configure correctly our application with Firebase.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjy969wrptdx94yrsgez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjy969wrptdx94yrsgez.png" alt="JSON File"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 2 - Integrate Firebase plugin
&lt;/h1&gt;

&lt;p&gt;To begin, we need to install &lt;a href="https://github.com/capacitor-community/fcm" rel="noopener noreferrer"&gt;Capacitor Firebase Cloud Messaging plugin&lt;/a&gt;. You can do with a simple command:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;npm i @capacitor-community/fcm&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Open your &lt;strong&gt;App.vue&lt;/strong&gt; file with your code editor. And... Oh, wait!&lt;/p&gt;

&lt;p&gt;Thanks to &lt;strong&gt;Elbphilharmonie&lt;/strong&gt; via &lt;a href="https://stackoverflow.com/questions/66206543/capacitor-typeerror-cannot-read-property-gettoken-of-undefined" rel="noopener noreferrer"&gt;StackOverFlow&lt;/a&gt;&lt;br&gt;
We now know how to set it correctly and avoid the result of &lt;strong&gt;undefined&lt;/strong&gt;. &lt;br&gt;
&lt;em&gt;I had it too&lt;/em&gt;&lt;br&gt;
First, we need to import FCM by add this line to &lt;strong&gt;App.vue&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { FCM } from '@capacitor-community/fcm';&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Then after components, add this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  data() {
    return {
fcm: new FCM()
    }
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, we have avoided this potential issue. But we need to execute it to generate a token and communicate with &lt;a href="https://capacitorjs.com/docs/apis/push-notifications" rel="noopener noreferrer"&gt;Push Notifications&lt;/a&gt; from Capacitor.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 3 - Integrate Capacitor Plugin
&lt;/h1&gt;

&lt;p&gt;Let's begin by importing Push Notifications' plugin. Add this line to &lt;strong&gt;App.vue&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { Plugins } from '@capacitor/core';&lt;br&gt;
const { PushNotifications } = Plugins;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;And a &lt;a href="https://v3.vuejs.org/api/options-lifecycle-hooks.html#mounted" rel="noopener noreferrer"&gt;mounted&lt;/a&gt; method with this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Request permission to use push notifications
    // iOS will prompt user and return if they granted permission or not
    // Android will just grant without prompting
PushNotifications.requestPermission().then(result =&amp;gt; {
      alert("result " + JSON.stringify(result));
    });
         // Add registration error if there are.
        PushNotifications.addListener("registrationError", (error) =&amp;gt; {
          console.log(`error on register ${JSON.stringify(error)}`);
        }),
        // Add Notification received
          PushNotifications.addListener(
            "pushNotificationReceived",
            (notification) =&amp;gt; {
              console.log(`notification ${JSON.stringify(notification)}`);
            }
          ),
          // Add Action performed
          PushNotifications.addListener(
            "pushNotificationActionPerformed",
            async (notification) =&amp;gt; {
                alert("notification " + notification)
              console.log("notification succeeded");
            }
          ),
          // Initialize the registration with FCM Token
          PushNotifications.register();
        const fcmToken = this.fcm.getToken();
       alert(JSON.stringify(fcmToken));
        console.log("token:" + JSON.stringify(fcmToken));
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; You cannot test this function with a web browser. Because FCM and Push Notifications plugin doesn't have a web implementation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4 - Build Android app
&lt;/h1&gt;

&lt;p&gt;We need to add support for the Android platform. Let's do it by following the &lt;a href="https://ionicframework.com/docs/vue/your-first-app/6-deploying-mobile" rel="noopener noreferrer"&gt;Ionic documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For your convenience, I leave here all the steps that you need.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ionic build
ionic cap add android
ionic cap copy
ionic cap sync
ionic cap open android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before you build the app, you need first to copy the file &lt;strong&gt;google-services.json&lt;/strong&gt; from Firebase into &lt;strong&gt;android/app&lt;/strong&gt;. Then on Android Studio, &lt;br&gt;
select app -&amp;gt; the folder with the name of your app -&amp;gt; &lt;strong&gt;MainActivity.java&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And add the following code after&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;import java.util.ArrayList;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import com.getcapacitor.community.fcm.FCMPlugin;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then into &lt;strong&gt;// Initializes the Bridge&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add(FCMPlugin.class);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5 - Optional
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Is your MainActivity.java flagged with the red color?&lt;/strong&gt; Something went wrong and the main issue is Android Studio isn't capable to load the package com.getcapacitor.community.fcm.FCMPlugin. &lt;br&gt;
Thanks to &lt;strong&gt;wekas&lt;/strong&gt; at &lt;a href="https://forum.ionicframework.com/t/capacitor-community-firebase-analytics-not-working/199294/3" rel="noopener noreferrer"&gt;Ionic Forum&lt;/a&gt;, we have the solution for this issue.&lt;br&gt;
On Android studio follow these steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Build -&amp;gt; Clean Project
File -&amp;gt; Sync Project With Gradle Files
Run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voilà! Now build your Android App and test it on your device.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6 - Test the notification
&lt;/h1&gt;

&lt;p&gt;We can send a test notification to see if everything is set up correctly.&lt;br&gt;
Connect to &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase&lt;/a&gt; select your project --&amp;gt; See all features --&amp;gt; Cloud Messaging -&amp;gt; Send your first message.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftpw1thbqu91fklln7llt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftpw1thbqu91fklln7llt.png" alt="Firebase Cloud Messaging"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set a title and text for it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdde9y2fzd9p3hafcltw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffdde9y2fzd9p3hafcltw.png" alt="Firebase Push Notification"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can try to send a push notification only on your device with the token. You can have it by debugging and look into the console. Follow the &lt;a href="https://ionicframework.com/docs/troubleshooting/debugging" rel="noopener noreferrer"&gt;Ionic Documentation&lt;/a&gt; for debugging your Ionic app.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fim0m53upcfcjgdzo0qpo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fim0m53upcfcjgdzo0qpo.png" alt="Firebase Token"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or you can set the notification by completing all the Firebase steps. This will work with all devices that have installed your Ionic app. No matter if your application is open or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vgcy835zkv5q5hxmfv9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vgcy835zkv5q5hxmfv9.jpg" alt="Success"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 7 - iOS
&lt;/h1&gt;

&lt;p&gt;I'm sorry, but currently, I cannot test the application on iOS. I will update as soon I can.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 8 - Source code
&lt;/h1&gt;

&lt;p&gt;Do you prefer to take a look at the code? Please see &lt;a href="https://github.com/marcoimproda/ionic-vue-push-notifications-demo" rel="noopener noreferrer"&gt;here&lt;/a&gt;. I have uploaded a demo application.&lt;br&gt;
Don't forget to upload your google-services.json and build &amp;amp; sync the code with Android Studio.&lt;/p&gt;

&lt;h1&gt;
  
  
  Thanks
&lt;/h1&gt;

&lt;p&gt;I hope this post could help everyone that needs to enable push notifications on their Ionic Vue application. I did my best to make it plain and simple and to provide a stable resource with an up-to-date tutorial to follow. That's it.&lt;br&gt;
Let me know if you find it helpful and until then, see you next time. Ciao :)&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://capacitorjs.com/docs/guides/push-notifications-firebase" rel="noopener noreferrer"&gt;Push Notifications with Ionic/Angular&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/techshots/learn-to-use-firebase-cloud-messaging-to-receive-push-notification-in-ionic-4-vue-app-using-13f5a4458b87" rel="noopener noreferrer"&gt;Firebase Cloud Messaging to receive Push Notification in Ionic 4 Vue app using Capacitor.&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/66206543/capacitor-typeerror-cannot-read-property-gettoken-of-undefined" rel="noopener noreferrer"&gt;Capacitor - TypeError: Cannot read property 'getToken' of undefined&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/capacitor-community/fcm/issues/5" rel="noopener noreferrer"&gt;Android Package io.stewan.capacitor.fcm.FCMPlugin does not exist&lt;/a&gt;&lt;br&gt;
&lt;a href="https://forum.ionicframework.com/t/capacitor-community-firebase-analytics-not-working/199294/3" rel="noopener noreferrer"&gt;Capacitor-community / firebase-analytics not working&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ionic</category>
      <category>vue</category>
      <category>firebase</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
