How to check error under register method of cordova push notification plugin using IONIC 2


#1

I have created a sample application in IONIC 2 for push notification services.
My code worked fine for ANDROID. But it didn’t work for iOS. Below are the points on which I focused for iOS.

  • Proper provisonal certification creation and ,p12 .
  • Settings in XCODE
  • Cloud settings in fire cloud messaging .
  • Application settings for iOS in ionic dashboard.

Below is my code.

constructor(public navCtrl: NavController,public push: Push) {
alert("home");
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
alert("Token is "+t.token
)
console.log('Token saved:', t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
alert(msg.title + ': ' + msg.text);
});
}'
}

I see only first alert. But I do not see the alert with token it doesn’t fire.
I am new with this. Can you guys please help me out.


#2

Where are you calling this code? It should definitely be wrapped inside a platform.ready() to prevent it from firing to early.


#3

This is my updated code

export class MyApp {
  rootPage = HomePage;
 
  constructor(platform: Platform, public push: Push) {
    platform.ready().then(() => {
      StatusBar.styleDefault();
      Splashscreen.hide();
      alert("here");
      this.push.register().then((t: PushToken) => {
        return this.push.saveToken(t);
      }).then((t: PushToken) => {
        alert('Token saved:'+ t.token);
      });
 
      this.push.rx.notification()
      .subscribe((msg) => {
        alert('I received awesome push: ' + msg);
      });
    });
  }
}

Please suggest me where should I focus to resolve this issue


#4

That’s not going to work. You’re starting with listening to notiications at the same time you’re trying to registering. You should await the then from register, since that’s the point where you truly know that someone is properly registered. In the then, you should invoke the listening (subscribe) to notifications.


#5

@luukschoen
Thanks for pointing the issue.
As I am following this tutorial example

https://devdactic.com/ionic-2-push-notifications/

As I am very new with IONIC, can you please write the accurate piece of code where I am doing it wrong. Thanks


#6

It should look something like this to make sure it’s fired in the correct order:

registerPush() {
    this.push.register().then((t: PushToken) => {
      return this.push.saveToken(t);
      }).then((t: PushToken) => {
        console.log('Token saved:', t.token);
        this.startListeningForPushNotifications();
    });
  }

  startListeningForPushNotifications(){
      this.push.rx.notification()
      .subscribe((msg) => {
        alert('I received awesome push: ' + msg);
      });
  }

#7

Thanks I will try this as well.

Just confirming, do I need to do something with cocoadova or do I need to work with ‘Add Firebase to your iOS app’ in firebase cloud messaging? When I have done all the iOS certification processes


#8

I tried as per your suggestion but bad luck I see nothing in log.