capacitor-community / fcm

Enable Firebase Cloud Messaging for Capacitor apps
https://capacitor.ionicframework.com/docs/
MIT License
238 stars 83 forks source link

Sample code #4

Closed mrahmadt closed 5 years ago

mrahmadt commented 5 years ago

Sharing my code as an example for anyone interested to use this plugin (please install the plugin and follow the instruction in the README file before using below code).

app.component.ts

initializeApp() {
    this.platform.ready().then(() => {
        if (Capacitor.isPluginAvailable('SplashScreen')) {
            Plugins.SplashScreen.hide();
        }
        if (Capacitor.isPluginAvailable('PushNotifications')) {
                PushNotifications.addListener('pushNotificationReceived', (notification: PushNotification) => {
                    alert('pushNotificationReceived ' + notification.body);
                    console.log('pushNotificationReceived ', notification.body);
                });
                PushNotifications.addListener('pushNotificationActionPerformed', (notification: PushNotificationActionPerformed) => {
                    alert('pushNotificationActionPerformed ' + JSON.stringify(notification));
                    console.log('notification ', JSON.stringify(notification));
                });
                PushNotifications.addListener('registrationError', (error: any) => {
                    alert('error on register ' + JSON.stringify(error));
                    console.log('error on register ',  JSON.stringify(error));
                });
        }
    });
  }

dashboard.page.ts

ngOnInit() {
        if (Capacitor.isPluginAvailable('PushNotifications')) {
        PushNotifications.register();
        PushNotifications.addListener('registration', (token: PushNotificationToken) => {
            console.log('FCM ',token.value);
        });
      }
}
stewones commented 5 years ago

Thanks, that's it 😁 can you PR a freshed ionic app under an example folder?

stewones commented 5 years ago

Added a demo app

https://github.com/stewwan/capacitor-fcm-demo