Send push notification on Skygear


#1

i’m using the onesignal plugin to get device token like :

getDeviceToken(){
this.oneSignal.getIds().then((tokenresponse)=>{
  console.log("Token " + tokenresponse.pushToken);
  this.token = tokenresponse.pushToken
  console.log("Token " + this.token);
  this.storage.set("token", tokenresponse.pushToken).then((response)=>{
      console.log("save token to storage");
  }).catch((error)=>{
    console.log("cant save token on storage");
  }); 
}).catch((error)=>{
    console.log("error from getToken ");
});

}

and atfer user login

setTimeout(()=>{ skygear.push.registerDevice(this.token, "android", "com.exemple.****").then((data)=>{ console.log("save token to skygear succeed"); }).catch((error)=>{ console.log("cant register token to skygear"); }); },5000)

and i try to send push like :

push(){
skygear.push.sendToDevice(
  ['token device'], // User IDs
  {
    'apns': {
        'aps': {
            'alert': {
                'title': "title",
                'body': "message",
            }
        },
        'from': 'skygear',
        'operation': 'notification',
    },
    'gcm': {
         'notification': {
              'title': "title",
              'body': "message",
          }
    },
  }
);

}


#2

If you’re using Skygear Push Notification, you should call Skygear’s skygear.registerDevice(token, type, topic); to save the token instead of this.storage.set("token")

type could be ios or android


#3

I get the token and save it to localStorage when i launch the app, After user login i get the token and save it to skygear. This.storage.set is for Dave thé token to localStorage


#4

@arsene may you post the code after login too?


#5
login(){
    let loading = this.loadingCtrl.create({
        content:'authentication...'
    });
    loading.present();
    skygear.auth.loginWithEmail(this.email, this.password).then((user) => {
      console.log(user); // user record
      console.log(user["email"]); // email of the user
      loading.dismiss();
      this.navCtrl.setRoot("HomePage");

    }).catch((error) => {
      console.error(error);
      loading.dismiss(); 
      setTimeout(()=>{
        loading.dismiss();
      });
      if (error.error.code === skygear.ErrorCodes.ResourceNotFound) {
        // the email has already existed
        setTimeout(()=>{
          loading.dismiss();
          let alert = this.alertCtrl.create({
            message:'Vous n\'etes pas inscrit',
            title:'Attention',
            buttons:[{
              text:'ok',
              handler:(()=>{
                console.log('ok'); 
              })
            }]
          })
          alert.present();
        });
      }
      if (error.error.code === skygear.ErrorCodes.InvalidCredentials) {
        // the email has already existed
        setTimeout(()=>{
          loading.dismiss();
          let alert = this.alertCtrl.create({
            message:'Erreur identifiant',
            title:'Attention',
            buttons:[{
              text:'ok',
              handler:(()=>{
                console.log('ok');
              })
            }]
          })
          alert.present();
        });
      }  
      else {
        // other kinds of error
        loading.dismiss();
      }
    });
  }

#6

Oh I intended to see how your registerDevice function integrate with the sign up or login process ot see if they’re registered correctly.

Or, did you see “save token to skygear succeed” in your chrome console?

Also for skygear.push.sendToDevice, did you put the device ID there? Coz I saw your comment say User IDs, which you should use skygear.push.sendToUser instead.


#7

yes I see that the recording goes in the chrome console
yes I set the phone’s token when I send a push to the phone


#8

#9

Can you send us a minimal code that demonstrates how it didn’t work so that we can help?


#10

I have no error rating phone but I can see the error in the server log


#11

the code I posted above and everything I try. can you try too please ?


#12

Hi there, I suspect it is because the device token is incorrect. We will provide some sample code on how to get the device token in Ionic demo for you later.


#13

Great ! Waiting for , what is the difference between sending to a user and to a device?


#14

@rickmak will update with the sample code.

Send to device meant send the push to a list of devices by device IDs; Send to user meant send the push to all devices owned by a list of user IDs.


#15

OK, waiting for this. Thanks for your assist


#16

we have a demo day tomorrow with our application, possible to work closely on the push notification before tomorrow?


#17

Hi @arsene,

I updated the ionic demo with actual working code for FCM. https://github.com/skygear-demo/skygear-ionic

The main files involved is /src/pages/push/push.ts

Please take a look and try out.

To make it actually working:

  1. Modify the endpoint at /src/app/skygear.service.ts
  2. Download the google-services.json from Firebase console, put the file under platforms/android
  3. ionic cordova run android

Let me know are you still got problems.

Best,
Rick


#18

Without any plugin ? I’ll do it. Thanks @rickmak


#19

hi @rickmak I just tested the code but I do not get the push!


#20

@arsene The sample project would work well if the push was correctly configurated. Did you mean you can’t make the sample projects works or you have another set of code?

If you’re trying out the sample projects, may you share more about how you configure the GCM?