Sandy
Sandy

Reputation: 210

How to open specific page from notification bar using ionic 3 and Firebase Cloud Messaging?

I already follow the instruction on Ionic 3 documentation about Push notification.

When I try to send the notification and my app on background, I cannot trigger 'notification' event so I cannot go through specific page.

But when my app is on foreground, the 'notification' event triggered automatically.

I use:

  1. Ionic 3 as the mobile framework

  2. Firebase Cloud Messagins as the message service

  3. Laravel with larave-fcm plugin to send message

Backend - Laravel code for push message to firebase

    $optionBuilder = new OptionsBuilder();
    $optionBuilder->setTimeToLive(60*20)->setContentAvailable(true);

    $notificationBuilder = new PayloadNotificationBuilder('Hello');
    $notificationBuilder->setBody('Hello world')->setSound('default');

    $dataBuilder = new PayloadDataBuilder();
    $dataBuilder->addData(['custom' => 'test']);

    $option = $optionBuilder->build();
    $notification = $notificationBuilder->build();
    $data = $dataBuilder->build();

    $token = "token";

    $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

    $success = $downstreamResponse->numberSuccess();
    $failure = $downstreamResponse->numberFailure();
    $modification = $downstreamResponse->numberModification();

    echo 'Success: '.$success;
    echo "<br>";
    echo 'Failure: '. $failure;
    echo "<br>";
    echo 'Modification: '.$modification;
    print_r($downstreamResponse->tokensToDelete());
    echo "<br>";
    print_r($downstreamResponse->tokensToModify());
    echo "<br>";
    print_r($downstreamResponse->tokensToRetry());
    echo "<br>";
    print_r($downstreamResponse->tokensWithError());

FRONTEND - My ionic apps constructor on app.component.ts

constructor(private translate: TranslateService, private platform: Platform, settings: Settings, private config: Config, private statusBar: StatusBar, private splashScreen: SplashScreen, public push: Push, public alertCtrl: AlertController, public storage: Storage, private backgroundMode: BackgroundMode) {
this.initTranslate();
this.platform.ready().then(() => {
  if(!this.backgroundMode.isActive) {
    this.backgroundMode.setDefaults({silent: true});
    this.backgroundMode.enable();
  } else {
    this.backgroundMode.disable();
    this.backgroundMode.setDefaults({silent: true});
    this.backgroundMode.enable();
  }      
  this.pushSetup();
  this.storage.get('test').then((val) => {
    if(val == 'news'){
      this.nav.setRoot(TabsPage);
    }
  });         
});
}

Function pushSetup()

pushSetup() {
const options: PushOptions = {
  android: {
      senderID: '10524067XXXXX'
  },
  ios: {
      alert: 'true',
      badge: true,
      sound: 'false'
  },
  windows: {}
};

const pushObject: PushObject = this.push.init(options);

pushObject.on('notification').subscribe((notification: any) => {
  if(notification.additionalData.foreground){
    let myAlert = this.alertCtrl.create({
      title: 'Push',
      message: JSON.stringify(notification)
    });
    myAlert.present();
  } else {        
    this.storage.set('test', 'news');
  }
});
pushObject.on('registration').subscribe((registration: any) => {
   console.log(registration);
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}

Upvotes: 1

Views: 887

Answers (1)

Sandy
Sandy

Reputation: 210

I found the answer from here.

I should send to fcm with like this

{
"data" : {
    "title": "Test Notification",
    "body": "This offer expires at 11:30 or whatever",
    "notId": 10,
    "surveyID": "ewtawgreg-gragrag-rgarhthgbad"
}
}

On laravel fcm just set title, body, notId on addData function from PayloadDataBuilder.

Upvotes: 2

Related Questions