Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Add external user ID to OneSignal

In order to be able to send transactional, or user-specific notifications to your users, you will need a way to identify which device belongs to each which user.

The nativeFunctions.onesignalSetExternalUserId() function allows you to assign a custom ID to the device in OneSignal, which can be the same ID used on your website to identify users, making it very easy for you to know to whom a specific device belongs, and allowing you to send user-specific notifications to that user.

We recommend triggering this function whenever you perform a check to see if the user is logged-in, so you can assign his user ID to OneSignal accordingly, here is an example of how to implement the Javascript code:

<script type="text/javascript">
    // Get the user agent and convert it to lowercase for case-insensitive comparison
    var userAgent = navigator.userAgent.toLowerCase();
    
    // Get the user ID
    var userId = "1";

    try {
        // Check if the user agent contains the string "canvas"
        if (userAgent.indexOf('canvas') > -1) {
            // Native function can be called here
            nativeFunctions.onesignalSetExternalUserId(userId);
        }
    } catch (ex) {
        // Log any error messages for easier debugging
        console.log(ex.message);
    }
</script>

OneSignal has a nice guide with more details on how to send those push notifications using the OneSignal REST API, you can find it here: https://documentation.onesignal.com/docs/transactional-messages