Push notification to specific app


#1

Hello,

Implementation below sends notification from my Google Firebase account Cloud Messaging to my app, but I’m looking for some useful example, how to push notification in Ionic2 Type Script Android application from one user specific app to another specific app :

my Installs:

    $ ionic cordova platform add android
    $ ionic cordova plugin add phonegap-plugin-push --variable SENDER_ID=my sender ID
    $ npm install --save @ionic-native/push

app.component.ts:

import { Component, ViewChild } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { AlertController, Nav } from "ionic-angular";
import { HomePage } from '../pages/home/home';
import { DetailsPage } from '../pages/details/details';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  rootPage: any = HomePage;

  constructor(platform: Platform, 
              statusBar: StatusBar, 
              splashScreen: SplashScreen, 
              private push: Push, 
              public alertCtrl: AlertController) {
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();

      this.initPushNotification();
    });
  }

  initPushNotification() {
    this.push.hasPermission()
      .then((res: any) => {
        if (res.isEnabled) {
          console.log('We have permission to send push notifications');
        } else {
          console.log('We don\'t have permission to send push notifications');
        }
      });

    const options: PushOptions = {
      android: {
        senderID: 'my sender ID'
      },
      ios: {
        alert: 'true',
        badge: true,
        sound: 'false'
      },
      windows: {}
    };
    const pushObject: PushObject = this.push.init(options);
    pushObject.on('notification').subscribe((notification: any) => {
      console.log('Received a notification', notification);
      let confirmAlert = this.alertCtrl.create({
        title: 'New Notification',
        message: JSON.stringify(notification),
        buttons: [{
          text: 'Ignore',
          role: 'cancel'
        }, {
          text: 'View',
          handler: () => {
            this.nav.push(DetailsPage, { message: notification.message });
          }
        }]
      });
      confirmAlert.present();
    });
    pushObject.on('registration').
      subscribe((registration: any) =>
        console.log('Device registered', registration));
    pushObject.on('error').
      subscribe(error =>
        console.error('Error with Push plugin', error));
  }
}

Example or advice would be helpful


Application has stopped error with Firebase Push notification receiving