One signal PostNotification problem

i’m trying to push notification using OneSignal native plugin but when i trying to use notification from user to user using postNotification Method it give me Error on the paramter Object

    this.oneSignal.getIds().then(ids => {
      console.log(ids);
      var notificationObj = { contents: {en: "message body"},
      include_player_ids: [ids.userId]};
      this.oneSignal.postNotification(notificationObj).then(res => {});
    });

give me error on notificationObj and the error is

Argument of type '{ contents: { en: string; }; include_player_ids: string[]; }' is not assignable to parameter of type 'OSNotification'.
  Property 'isAppInFocus' is missing in type '{ contents: { en: string; }; include_player_ids: string[]; }'.

so what is the solution of this ?

1 Like

are you retrieving the app id? I guess you don’t need this part

" { contents: {en: “message body”} "

just pass the id to a variable…

can you give me code example ?

I don’t know in Ionic 3 but in version 1 it goes like this

window.plugins.OneSignal.getIds(function(ids) {
   $rootScope.player_id = ids.userId;
});

Just change it to typescript…

you miss understand me , i need to send notification to user from typescript using post notification not just retrieving the ids for the user

initializeApp() {
    this._platform.ready().then(() => {
      this._OneSignal.startInit('yyyyyy', 'xxxx');
      this._OneSignal.inFocusDisplaying(this._OneSignal.OSInFocusDisplayOption.Notification);
      this._OneSignal.setSubscription(true);
      this._OneSignal.handleNotificationReceived().subscribe(() => {
        // handle received here how you wish.
      });
      this._OneSignal.handleNotificationOpened().subscribe(() => {
        // handle opened here how you wish.
      });
      this._OneSignal.endInit();        
    })    
  }

i need to make a new notification using in type Script using postNotification method in Sdk .
this part of code already exists in my application

@YousefRabieKhalil following this tutorial http://www.codingandclimbing.co.uk/blog/ionic-2-setup-push-notifications-for-android-with-onesignal

ohhh that’s why I’m asking first if you are just retrieving app_id but anyways you need http request for that… you may refer from this https://documentation.onesignal.com/reference#create-notification. Basically you need to pass data

app_id: your_app_ID,
include_player_ids: user_app_id_to_send,
contents: {
     en: "Test message"
 },
 headings: {
     en: "Test Heading"
}
1 Like

thank you this is relay help me :smiley:

is your problem solved? if yes please post your solution for others to be able to have hint/examples if they also having the same problem…

i will , i think the postNotification method in Onesingal native plugin is broken , so just using the rest api for post a new notification using http post

  MakeAnewNotification() {
    this.oneSignal.getIds().then(ids => {
      var body = {
        app_id: 'Your app Id',
        include_player_ids: [ids.userId],
        contents: {
          en: "Test message"
        },
        headings: {
          en: "Test Heading"
        }
      };
      this.http.post('https://onesignal.com/api/v1/notifications', body).subscribe(data => {
        console.log(data);
      } , error => {
        console.log(error);
      });
    });
  }

first get the current user id using get id from Onesignal native plugin mention above then using http module in angular post a new request and follow this https://documentation.onesignal.com/reference#create-notification

nice catch, I’m glad you work it all around…