Web SDK Mapping

Maps method from our older Player Model SDK to User Model

The structure of this document is similar to the one in the original Player Model Web SDK Reference doc. The headers indicate the method(), property, and #event names from the Player Model, and the code snippet shows the differences between the two models. All snippets are presented in the Typescript programming language and may not represent complete working code. Please refer to the docs linked for each method/property to see examples in other languages and to learn more about it.

OneSignalSDKWorker.js

The OneSignal Service Worker code within the OneSignalSDKWorker.js file needs to be updated from:

Player Model code:

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

To User Model code:

importScripts("https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.sw.js");

The location and path of the file should stay the same. Just update this one line.

See OneSignal Service Worker for details.

Initialization

init()

Player Model doc

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<script>
window.OneSignal = window.OneSignal || [];
OneSignal.push(function() {
    OneSignal.init({
      appId: "YOUR_APP_ID"
    });
});
</script>

User Model doc

<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(async function(OneSignal) {
  await OneSignal.init({
    appId: "YOUR_APP_ID",
  });
});
</script>

provideUserConsent()

Player Model doc

OneSignal.provideUserConsent(true)

User Model doc

OneSignal.setConsentGiven(true)

Registering Push

showNativePrompt()

Player Model doc

OneSignal.showNativePrompt()

User Model doc

OneSignal.Notifications.requestPermission()

registerForPushNotifications() [dropped]

Player Model doc

OneSignal.registerForPushNotifications()

#permissionPromptDisplay

Player Model doc

OneSignal.on('permissionPromptDisplay', () => ... })

User Model doc

OneSignal.Notifications.addEventListener('permissionPromptDisplay', event => { ... })

showSlidedownPrompt()

Player Model
doc

OneSignal.showSlidedownPrompt()

User Model doc

OneSignal.Slidedown.promptPush()

showHttpPrompt() [dropped]

Player Model
doc

OneSignal.showHttpPrompt()

showCategorySlidedown()

Player Model doc

OneSignal.showCategorySlidedown()

User Model doc

OneSignal.Slidedown.promptPushCategories()

#getNotificationPermission

Player Model doc

OneSignal.on('getNotificationPermission', (permission) => ... })

User Model doc

OneSignal.User.PushSubscription.addEventListener('change', ({ optedIn }) => { ... })

isPushNotificationsSupported()

Player Model doc

OneSignal.isPushNotificationsSupported()

User Model doc

OneSignal.Notifications.isPushSupported()

isPushNotificationsEnabled()

Player Model doc

await OneSignal.isPushNotificationsEnabled()

User Model doc

OneSignal.User.PushSubscription.optedIn

#subscriptionChange

Player Model doc

OneSignal.on('subscriptionChange', (subscribed) => ... })

User Model doc

OneSignal.User.PushSubscription.addEventListener('change', ({ token }) => { ... })

Analytics

#notificationPermissionChange

Player Model doc

OneSignal.on('notificationPermissionChange', ({ to }) => ... })

User Model doc

OneSignal.Notifications.addEventListener('permissionChange', permission => { ... })

#popoverShown

Player Model doc

OneSignal.on('popoverShown', () => ... })

User Model doc

OneSignal.Slidedown.addEventListener('slidedownShown', presented => { ...})

#customPromptClick

Player Model doc

OneSignal.on('customPromptClick', ({ result }) => ... })

User Model doc

 OneSignal.Notifications.addEventListener('click', ({notification, result}) => { ... })

User IDs

getUserId

Player Model doc

OneSignal.getUserId()

User Model doc

OneSignal.User.PushSubscription.id;

setExternalUserId()

Player Model doc

OneSignal.setExternalUserId("external id")

User Model doc

OneSignal.login("external id")

removeExternalUserId()

Player Model doc

OneSignal.removeExternalUserId()

User Model doc

OneSignal.logout()

getExternalUserId()

Player Model
doc

await OneSignal.getExternalUserId()

User Model doc

OneSignal.User.externalId

Tags

sendTag()

Player Model doc

OneSignal.sendTag("key", "value")

User Model doc

OneSignal.User.addTag("key", "value")

sendTags()

Player Model doc

OneSignal.sendTags({ key1: 'value1', key2: 'value2' })

User Model doc

OneSignal.User.addTags({ key1: 'value1', key2: 'value2' })

getTags()

Player Model doc

await OneSignal.getTags()

User Model doc

OneSignal.User.getTags()

deleteTag()

Player Model doc

OneSignal.deleteTag("key")

User Model doc

OneSignal.User.removeTag("key")

deleteTags()

Player Model doc

OneSignal.deleteTags(["key1", "key2"])

User Model doc

OneSignal.User.removeTags(["key1", "key2"])

Push Notifications

sendSelfNotification() [dropped]

Player Model doc

OneSignal.sendSelfNotification('title', 'message', 'url') 

setSubscription()

Player Model doc

OneSignal.setSubscription(false)

User Model

OneSignal.Notifications.permission = false

Receiving Notifications

#notificationDisplay

Player Model doc

OneSignal.on('notificationDisplay', (event) => { ... })

User Model doc

OneSignal.Notifications.addEventListener('foregroundWillDisplay', ({ notification ) => { ... })

#notificationDismiss

Player Model doc

OneSignal.on('notificationDismiss', (event) => { ... })

User Model doc

OneSignal.Notifications.addEventListener('dismiss', ({ notification }) => { ... })

#addListenerForNotificationOpened

Player Model doc

OneSignal.on('addListenerForNotificationOpened', (event) => { ... })

User Model doc

// TODO

Email

setEmail()

Player Model doc

OneSignal.setEmail('[email protected]')

User Model doc

OneSignal.User.addEmail('[email protected]')

logoutEmail()

Player Model doc

OneSignal.logoutEmail()

User Model doc

OneSignal.User.removeEmail('[email protected]')

getEmailId() [dropped]

Player Model doc

await OneSignal.getEmailId()

SMS

setSMSNumber()

Player Model doc

OneSignal.setSMSNumber('+11234567890')

User Model doc

OneSignal.User.addSms('+11234567890')

logoutSMSNumber()

Player Model doc

OneSignal.logoutSMS()

User Model doc

OneSignal.User.removeSms('+11234567890')