Chrome Apps / Extensions SDK

OneSignal Chrome Apps / Extensions SDK Reference. Works with Chrome Apps / Extensions.

🚧

Not for Chrome Web Push

Chrome Apps & Extensions are not web push, which is available here.

Name

Type

Description

Initialization

init

Function

Initialize OneSignal

User IDs

getIdsAvailable

Function

Get the user ID of the device

idsAvailableCallBack

Tags

getTags

Function

View tags from a user

tagsReceivedCallBack

Callback

sendTag

Function

Add a tag to a user

sendTags

Function

deleteTag

Function

Delete a tag from a user

deleteTags

Function

Receiving Notifications

addListenerForNotificationOpened

Function

When a user takes an action on a notification

notificationOpenedCallBack

Callback

Initialization

init

Function

Only required method you need to call to setup OneSignal to receive push notifications. Call this from your background.js file outside of any other functions.

Parameter

Type

Description

appId

String

Required Your OneSignal App Id, found in Keys & IDs.

googleProjectNumber

String

Required Your Google Project number.

OneSignal.init({appId: "5eb5a37e-b458-11e3-ac11-000c2940e62c",
                 googleProjectNumber: "703322744261"});

User IDs

getIdsAvailable

Function

Lets you retrieve the OneSignal User ID and the Google Registration ID. Your handler is called after the device is successfully registered with OneSignal.

Parameter

Type

Description

idsAvailableCallBack

Function

Passed in function which will receive one parameter containing JSON with userId and registrationId.

OneSignal.getIdsAvailable(function(ids) {
    console.log("getIdsAvailable:"
                    + "\nUserID: " + ids.userId
                    + "\nRegistration ID: " + ids.registrationId);
  });

idsAvailableCallBack

Callback

Get the OneSignal userId and the Google Registration ID.

Parameter

Type

Description

userId

String

OneSignal userId is a UUID formatted string.(unique to a Chrome user per device per app)

registrationId

String

Google assigned identifier(unique to a Chrome user per device per app and changes on every reinstall).

NOTE: Might be blank if there was a connection issue.

OneSignal.getIdsAvailable(function(ids) {
    console.log("getIdsAvailable:"
                    + "\nUserID: " + ids.userId
                    + "\nRegistration ID: " + ids.registrationId);
  });

Tags

getTags

Function

Retrieve a list of tags that have been set on the user from the OneSignal server.

Parameter

Type

Description

tagsReceivedCallBack

Function

Passed in function which will receive one parameter containing JSON.

OneSignal.getTags(function(tags) {
        console.log("OneSignal getTags:");
        console.log(tags);
    });

tagsReceivedCallBack

Callback

Gets all the tags set on a user from onesignal.com.

Parameter

Type

Description

tags

JSON

JSON object of key value pairs retrieved from the OneSignal server.

OneSignal.getTags(function(tags) {
        console.log("OneSignal getTags:");
        console.log(tags);
    });

sendTag

Function

Tag a user based on an app event of your choosing so that later you can create segments in Segments to target these users. Recommend using sendTags over sendTag if you need to set more than one tag on a user at a time.

Parameter

Type

Description

key

String

Key of your choosing to create or update.

value

String

Value to set on the key.
NOTE: Passing in a blank String deletes the key, you can also call deleteTag.

OneSignal.sendTag("level", "64");

sendTags

Function

Tag a user based on an app event of your choosing so later you can create segments on onesignal.com to target these users.

Parameter

Type

Description

keyValues

JSON

Key value pairs of your choosing to create or update. NOTE: Passing in a blank String as a value deletes the key, you can also call deleteTag or deleteTags.

OneSignal.sendTags({level: "64", coins: "101"});

deleteTag

Function

Deletes a tag that was previously set on a user with sendTag or sendTags. Use deleteTags if you need to delete more than one.

Parameter

Type

Description

key

String

Key to remove.

OneSignal.deleteTag("key");

deleteTags

Function

Deletes tags that were previously set on a user with sendTag or sendTags.

Parameter

Type

Description

keys

Array

Keys to remove.

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

Receiving Notifications

addListenerForNotificationOpened

Function

Passed-in callback that fires when the user clicks on a OneSignal notification.

Parameter

Type

Description

notificationOpenedCallBack

Function

Passed in function which will receive one parameter containing JSON with data about the notification that was clicked.

OneSignal.addListenerForNotificationOpened(function(data) {
    console.log("Received NotificationOpened:");
    console.log(data);
});

notificationOpenedCallBack

Callback

Function that gets called when a OneSignal notification is opened. Function will receive one parameter with JSON about the notification.

Parameter

Type

Description

title

String

The message title text the user seen in the notification.

message

String

The message body text the user seen in the notification.

icon

String

Icon set on the notification.

additionalData

JSON

Key value pairs that were set on the notification.

actionButtons

Array

Any buttons that were present on the notification.

actionSelected

String

Id of the button on the notification that was clicked.

OneSignal.addListenerForNotificationOpened(function(data) {
    console.log("Received NotificationOpened:");
    console.log(data);
});

Did this page help you?