Getting OneSignal UserID with Unity (C#)

2019-08-22 01:33发布

问题:

In my app, I have some push notification subscribers (OneSignal). I am trying to get the user id's like this:

OneSignal.GetIdsAvailable(IdsAvailable);
....
....
private void IdsAvailable(string userID, string pushToken)
{
    playeridx = userID;
    System.Diagnostics.Debug.WriteLine("UserID:" + userID);
    System.Diagnostics.Debug.WriteLine("pushToken:" + pushToken);
}

But I get an error: "OneSignal does not contain a definition for GetIdsAvailable."

Any ideas?

回答1:

It looks like you are using a method found in the OneSignal Windows SDK. If you want to do this in Unity, setup the Unity SDK (if you haven't done so already) and then perhaps use GetPermissionSubscriptionState to retrieve the userId and pushToken of the current user, instead of using .GetIdsAvailable().



回答2:

As I am seeing, we must use the code bellow to get the UserID. But, which event is triggered when the UserID arrives from Onesignal? if I use the UserID right after this line, it always has the UserID? It sounds strange for me, because, sometimes it could not had received the UserID yet.

OSPermissionSubscriptionState state = OneSignal.GetPermissionSubscriptionState();
extraMessage += " UserId: "+state.subscriptionStatus.userId;