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.

NameTypeDescription
Initialization
initFunctionInitialize OneSignal
User IDs
getIdsAvailableFunctionGet the user ID of the device
idsAvailableCallBack
Tags
getTagsFunctionView tags from a user
tagsReceivedCallBackCallback
sendTagFunctionAdd a tag to a user
sendTagsFunction
deleteTagFunctionDelete a tag from a user
deleteTagsFunction
Receiving Notifications
addListenerForNotificationOpenedFunctionWhen a user takes an action on a notification
notificationOpenedCallBackCallback

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.

ParameterTypeDescription
appIdStringRequired Your OneSignal App Id, found in Keys & IDs.
googleProjectNumberStringRequired 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.

ParameterTypeDescription
idsAvailableCallBackFunctionPassed 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.

ParameterTypeDescription
userIdStringOneSignal userId is a UUID formatted string.(unique to a Chrome user per device per app)
registrationIdStringGoogle 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.

ParameterTypeDescription
tagsReceivedCallBackFunctionPassed 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.

ParameterTypeDescription
tagsJSONJSON 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.

ParameterTypeDescription
keyStringKey of your choosing to create or update.
valueStringValue 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.

ParameterTypeDescription
keyValuesJSONKey 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.

ParameterTypeDescription
keyStringKey to remove.
OneSignal.deleteTag("key");

deleteTags

Function

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

ParameterTypeDescription
keysArrayKeys to remove.
OneSignal.deleteTags(["key1", "key2"]);

Receiving Notifications

addListenerForNotificationOpened

Function

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

ParameterTypeDescription
notificationOpenedCallBackFunctionPassed 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.

ParameterTypeDescription
titleStringThe message title text the user seen in the notification.
messageStringThe message body text the user seen in the notification.
iconStringIcon set on the notification.
additionalDataJSONKey value pairs that were set on the notification.
actionButtonsArrayAny buttons that were present on the notification.
actionSelectedStringId of the button on the notification that was clicked.
OneSignal.addListenerForNotificationOpened(function(data) {
    console.log("Received NotificationOpened:");
    console.log(data);
});