Associating Users and Devices

You can associate your app users and their devices via the OneSignal Player Id (oneSignalUserId) and then send push notifications to specific devices. Refer to OneSignal's documentation on Player Id

📘

External User ID

Associating users and devices gives full control over how you associate between users and devices, and to which devices you send notifications. However, an alternate strategy to send personalized push notifications is to use OneSignal's external user id functionality as discussed under Personalized Push.

There are two methods to associate users and devices. The first uses JavaScript on your website to obtain the device player id, the second uses an API endpoint on your server to POST the same data.

Method #1 - Javascript

On an appropriate page on your website, such as a login success page, define a function median_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 median_onesignal_info(oneSignalInfo) {
    console.log(oneSignalInfo);
}

// oneSignalInfo contains the below
{
    oneSignalUserId: 'xxxxxxx',
    oneSignalPushToken: 'xxxxxx',
    oneSignalSubscribed: true,
    oneSignalRequiresUserPrivacyConsent: false,
    platform: 'ios',
    appId: 'io.median.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 Median 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.