Skip to main content

OneSignal

OneSignal

WebSocket Version Guide
This functionality available only with WebSocket Version
REQUIREMENTS

To be able to implement this guide, you need to learn how to insert PHP snippets to your website.

You can find guide here: WP Beginner

Compatibility
This guide compatible with Better Messages 2.1.2 or higher

Installation

1. How to install OneSignal

  1. Download OneSignal from official website and install following standard WordPress installation process
  2. Configure OneSignal plugin following official guide

2. Configure OneSignal to assign external user id

By default, OneSignal WordPress plugin does not assign external user id to web push subscription, but that is required for sending private messages notifications, so we need configure that.

It is possible by adding this PHP snippet to your WordPress installation:

<?php
add_action('wp_footer', 'onesignal_assign_user_id', 999 );

function onesignal_assign_user_id(){
if( ! is_user_logged_in() ) return;
?>
<script type="text/javascript">
if( typeof OneSignal !== 'undefined' ){
function updateOneSignalID(){
var uid = '<?php echo get_current_user_id() ?>';
OneSignal.getExternalUserId().then(function(user_id){
if( uid !== user_id ){
OneSignal.setExternalUserId(uid);
}
} )
}

OneSignal.push(function() {
updateOneSignalID();

OneSignal.on('subscriptionChange', function (isSubscribed) {
if( isSubscribed ) updateOneSignalID();
})
});
}
</script>
<?php
}

After this configuration WordPress user id must be automatically assigned to OneSignal web push subscriptions when user visiting your website logged in and Better Messages should start to send web push notifications to offline users automatically.