Open detail page on OneSignal Push Notification pushed


#1

Hi,

I’m trying to open a detail page based on the payload extra data within a received OneSignal Push notification.

This is my code that is in my constructor of app.component.ts after app is ready()

export class myapp{
  @ViewChild(Nav) nav: Nav;
....
constructor(){
.........
this.notificationOpenedCallback = function(jsonData) {
        let seccion= JSON.parse(JSON.stringify(jsonData)).notification.payload.additionalData.seccion;
        let _id= JSON.parse(JSON.stringify(jsonData)).notification.payload.additionalData._id;

        switch (seccion){
          case "documentacion":
            this.nav.setRoot(DocumentationListPage,{_id: _id});
            break;
          case "permanencia":
            this.nav.setRoot(PermanenciasListPage,{_id: _id});
            break;
          case "seguro":
            this.nav.setRoot(InsuranceListPage,{_id: _id});
            break;
          case "ticket":
            this.nav.setRoot(TicketsListPage,{_id: _id});
            break;
        }
      };

When I click on my notification this error is launched:

console.log: Error in Success callbackId: OneSignalPush1032775348 : TypeError: Cannot read property 'nav' of
            null

What would be doing wrong?

Thanks


#2

Change

this.notificationOpenedCallback = function(jsonData) {

to

this.notificationOpenedCallback = jsonData => {

Any reason you’re doing this?

JSON.parse(JSON.stringify(jsonData))

Is it because it says the property doesn’t exist? If so, do this

this.notificationOpenedCallback = (jsonData: any) => {

or

this.notificationOpenedCallback = (jsonData: {[s: string]: any}) => {

Why are you defining the method in this fashion though? Inside the constructor? :slight_smile: If it’s because of the variables you’re defining, you could just parse them as parameters and do

notificationOpenedCallback(seccion, _id) {
    switch (seccion){
        ...
    }
}

And call that wherever you’re currently defining the function

this.notificationOpenedCallback(jsonData.notification.payload.additionalData.seccion, jsonData.notification.payload.additionalData._id);

#3

Sorry, I’m some new on typescript / javascript and some times I do strange things :slight_smile:

Even so I defined in this fashion way because on the documentation of onesignal describes de process like this

I’m gonna try your solution and I’ll tell you


#4

Well,

this.notificationOpenedCallback = (jsonData: any) => {

It works, what is the difference? :blush:


#5

Did it fix the error? :slight_smile: It’s just expecting a different type, other than an object or an any, when nothing is defined. Bit odd really, if it doesn’t know what type something is, it should really assume any by default.


#6

Execution context binding.