Onesignal Plugin. How do I get the playerId from ONESIGNAL and store it in storage? Where am I making a mistake?

I have this code and I can get the ids.userId (Player ID) of the user from Onesignal and send it in notificationObj as contents. I am able to see the Player ID but the problem is after the notofocationObj. I can not assign it to this.playerID and this.showOSAlert will not fire-up. Anytime I use this. the function doesnt work. How do I get the Player Id and pass it to my application. I would like to keep it in a variable later then I will keep it in storage.


// send a notification with an image
sendNotificationwithImage() {
	window["plugins"].OneSignal.getIds(function(ids) {
		var notificationObj = { contents: {en: ids.userId},
                      include_player_ids: [ids.userId],
                      big_picture: "",
                      ios_attachments: {id1: ""}

				this.playerID= ids.userId;
  				this.showOSAlert('Player ID:', this.playerID, 'rrrrrrr')

		window["plugins"].OneSignal.postNotification(notificationObj,function(successResponse) {
  				console.log("Notification Post Success:", successResponse);
			function (failedResponse) {
  				console.log("Notification Post Failed: ", failedResponse);
  				alert("Notification Post Failed:\n" + JSON.stringify(failedResponse));

Try changing the “this.playerID” part…

this.playerID = notificationObj.contents.en.ids.userId

I have tried this and it didnt work. I assume its about the promises and resolve. Onesignal returns the Player ID correctly and I can see the PlayerID in console.log. The problem is getting it to a Global Variable. Anything in window[“plugins”].OneSignal… function will not except "this.PlayerId or this.anotherfunction… So it throws an error in console such as undefined is not an object. I assume this error is for the function. it simply cant fire it up like it s not there. Is this because I am running the window[“plugins”]. and cant interrupt the script to declare my variables?

in other words let playerID is ok but this.playerID is giving error. If I cant get there to grab the Player ID to put it in app storage then its just in console and cant use it…

Have you tried to save the player id in localstorage first and then build the notificationObj ?
By the way, why you are not using the ionic onesignal native ( or for v5 (

I can get the player id but I can not assign it to a global variable or I can not fire up any other script to save it to localstorage…

I am not using native, I have tried but it didnt work. I will later on change it.

The problem here I can see and I can console.log the player id but I can not save it. The windows plugin function works but anything in the function that will fireup the localstorage or Alertcontroller or else dont work. :slight_smile:

I assume I will go with the work around; I will add keys and tags from the user credentials to one signal such as email, order number, I will still try PlayerID as a key value (if I can push this??). Coz now It will be in onesignal window plugin way… Then I can save that tags in storage to use it later on., since they are user credentials from their account and the tag will be uniquely representing the user etc. Then I can send notifications to that user. This will be a custom notification specific to that user.

I wanted to send that player id to the Vendor and Vendor would reply to that specific user with one-signal notifications. There are a few ways with playerId or with tags or segments… I wanted to send the playerid since onesignal already captures and generates this. Now I will be creating custom PlayerId which is another option and send to custom playerIds or as I have explained I will create tags from the Order numbers or emails…

This is a wordpress backend REST API + Woocomerce app. At the time of there is a missing item the vendor will be refunding the woocomerce product and will offer substitute item. So it has to be a direct notification but with limited communications… Thx

If you want to keep the player id in a global variable you can use a service for this.

Create a service like this

import { Injectable } from '@angular/core';

export class PushService {
    public playerId: any;

Then inject the service into your component and use it to keep the player id.

import { PushService } from '../whatever/push.service.ts'

constructor (public pushService: PushService) { }

//Inside your send notification function
this.pushService.playerId = ids.userId;

You can inject the service on every component that you need to call the player id, it will be stored in “this.pushService.playerId”.

I will try this and will let you know. It sounds like a good approach. Thank You. Will let you know the result.

Never type the word ‘function’ inside of one.

Just i want to know about…this apps is really working fine…i want to know about that…can i use in wordpress website.

Thanks & Regards

Hey rapropos, are you referring to onesignal function? I have grabbed that from their page…I think this is a promise problem. I can see the playerid in console. After that point since it’s clearly there, I want to keep it in storage… anything that I tried with “this.” didnt work. Meaning; as soon as the playerId is resolved and I know its there the next line is this.PlayerId = status.subscriptionStatus.userId; It throws error; .this is unrecognized… Different approaches [object to Promise] error. Tried with return and tried with async await. tried .promise and subscribe :slight_smile: basically anything that goes in window[“plugins”].OneSignal.getIds(function(ids) {… if it is starting with this. then it throws error and async await doesnt work… Need help please

How do I get the playerId from ONESIGNAL and store it in storage??

Here is a simple sample and console.log shows this is working. I cant get the resolved data to my app…

FINALLLy This worked for me. I was able to put it in storage…
window[“plugins”].OneSignal.getIds((ids)=>this.showOSAlert(‘Player ID:’, ids.userId, ‘rrrrrrr’));

But later then I have changed and tried the native plugin as tokuhara suggested earlier and at the very beginning of discussion. That worked as well when replaced and reinstalled the plugin from the URL given above…

Thx Alll

I don’t think so.

You posted a function (that I assume you wrote) named sendNotificationwithImage. Inside it, the word function appears twice (and there’s one var, which should also never be used, but for different reasons that aren’t relevant here). Those functions are the source of your trouble, and if you read through a couple of the threads in the search history I posted as the link, you will understand exactly why.