Capturning Ionic push notifications in open app

In the final stretch of understanding/implementing Ionic2 push notifications.

Last stuck on GET https://api.ionic.io/push/notifications showing my POST https://api.ionic.io/push/notifications were all of status:locked, state:enqueued, and notifications NOT showing up on device and thinking notifications were not being sent.

Quickly learned that notifications WERE being sent, it was simply that my open app was not intercepting the notifications. (Notifications fly through predictably when app was CLOSED)

From Ionic Docs - Ionic Documentation picked up on:

Handling Notifications
To handle push notifications in your app, you need to subscribe to the notification observable.

this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ’ + msg.text);
});

However, I can’t get this to fire, can someone assist?

in app.ts I have:

platform.ready().then(() => {
  StatusBar.styleDefault();

  if (platform.is('mobileweb')) {
      this.devicePushToken = { id: '', registered: false, saved: false, token: '' };
      this.InitApp();
  } else {
      console.log('Device UUID: ' + Device.device.uuid);
      let saveOptions = { ignore_user: true };
      this.push.register().then((t: PushToken) => {
          return this.push.saveToken(t, saveOptions);
      }).then((t: PushToken) => {
          this.devicePushToken = t;
          console.log('Token saved:', t.token);

          this.push.rx.notification()
              .subscribe((msg) => {
                  alert(msg.title + ': ' + msg.text);
              });

          this.InitApp();
      });
  }

});

1 Like

I implemented ionic 2 push notifications and also capturing in open app to show alert to ignore or view notification. I wrote a small tutorial to help others, see if below link is useful

The docs for this are confusing… I have mine as:

          this.push.register().then( (t: PushToken) => {
            return this.push.saveToken(t);
          }).then( (t: PushToken) => {
            console.log('Token saved:', t.token);
          }).catch( err => { console.log("register or save error", err);} );

          this.push.rx.notification()
            .subscribe( (msg) => { console.log(msg); this.mpapi.receiveNotification(msg);  });
          console.log('Logged in');
        })

That is, the .rx call is outside the then() of the register() promise - this seems to work, for me.