Associating Users and Devices

Method #1 - Javascript

On an appropriate page on your website, such as a login success page, define a function gonative_onesignal_info(). If present this function will be run by your app each time the page is loaded. The function is called with a parameter object containing all the OneSignal info, including the OneSignal User ID as oneSignalUserId.

Note that on iOS the oneSignalUserId will not be set if the user has declined the consent to opt out of push notifications.

/* oneSignalInfo Object */
// You define this function on your page, but do not actually call it
// If present on a page it will be called by the app when the page is loaded
function gonative_onesignal_info(oneSignalInfo) {
    console.log(oneSignalInfo);
}

// oneSignalInfo contains the below
{
    oneSignalUserId: 'xxxxxxx',
    oneSignalPushToken: 'xxxxxx',
    oneSignalSubscribed: true,
    oneSignalRequiresUserPrivacyConsent: false,
    platform: 'ios',
    appId: 'io.gonative.example',
    appVersion:  '1.0.0',
    distribution: 'release',
    hardware: 'armv8',
    installationId: 'xxxx-xxxx-xxxx-xxxx',
    language: 'en',
    model: 'iPhone',
    os: 'iOS',
    osVersion: '10.3',
    timeZone: 'America/New_York'
}
/* Login Page Sample Code */
var gn_oneSignalUserId;

function gonative_onesignal_info(oneSignalInfo){
  gn_OneSignalUserId = oneSignalInfo.oneSignalUserId;
}

function yourLoginFunction(){
  if(gn_OneSignalUserId){
    $.ajax({
      url: updateUserRecord,
      type: "POST",
      data: {
        yourAppUserId: userId,
        oneSignalUserId = gn_OneSignalUserId
      },
      contentType: "application/json"
    });
  }
}

Method #2 - POST to API endpoint

In the GoNative App Builder define an API endpoint URL which the app will send a POST request to that includes the user's push token. The POST request will include any session cookies and complete HTTP headers. These can be used in your back-end to identify the user and match the push token and user record.

The POST request is sent:

  1. When the app registers with OneSignal and push tokens are initially created
  2. When a page that matches the URL defined is loaded in the app (e.g. a login success page)

📘

Testing Tips

We strongly recommend specifying .* or All Pages for testing until you have verified you can receive a POST. This will POST the data on all URLs within your app. If you still do not receive the POST verify your endpoint by testing with a tool like cURL or Postman.