Web SDK Chrome Extension API
Initialize the SDK
Interface Description
To prevent a large number of invalid users, it is recommended to use a unique and consistent user_str
during initialization. You can generate user_str
based on the browser fingerprint.
Example Call
if (window.MTpushInterfaceExtension) {
window.MTpushInterfaceExtension.initSdk({
appkey: '',
user_str: '',
});
}
Parameter Description
interface InitType {
appkey: string; // The appkey of the application registered by the developer on the EngageLab platform. Required.
user_str: string; // Unique user identifier. Required.
}
Get Results
chrome.runtime.onMessage.addListener((message) => {
switch (message.type) {
// SDK initialization succeeded
case 'MTPUSH_INIT_SDK_SUCCESS':
console.log(message.data);
break;
// SDK initialization failed
case 'MTPUSH_INIT_SDK_FAIL':
console.log(message.data);
break;
}
});
The structure of message.data
is as follows:
interface MessageData {
code: number; // Status code. 0 indicates success, others indicate failure. Refer to error codes.
message: string; // Result description.
content: string; // Returns failure information when registration fails with error code 1003.
}
If the integration of the extension fails due to an expired free trial or paid plan for the application, the initialization process will be retried when a new page is opened or an existing page is refreshed after 4 hours.
Get RegistrationID
Interface Description
There's no need to manually call an API to get the regid
. After the SDK initializes successfully, you can obtain the regid
from the information returned by the MTPUSH_GET_MT_INIT_INFO
event.
Example Call
chrome.runtime.onMessage.addListener((message) => {
switch (message.type) {
case 'MTPUSH_GET_MT_INIT_INFO':
console.log(message.data.regid);
break;
}
});
Return Result
interface MTInitInfo {
website_push_id: string;
code: number;
master_secret: string;
passwd: string;
pull: number;
regid: string;
sess: string;
tagalias: number;
uid: number;
vapid_pubkey: string;
}
Stop Push Notifications
Interface Description
Call this API to disconnect the persistent push connection established with the backend and stop receiving push messages.
Example Call
window.MTpushInterfaceExtension.stopPush();
Check Push Service Status
Interface Description
Retrieve the status information of the push service.
Example Call
// Call after the SDK initializes successfully
window.MTpushInterfaceExtension.getPushAuthority();
// Listen for messages to get the push service status
chrome.runtime.onMessage.addListener((message) => {
switch (message.type) {
case 'MTPUSH_GET_PUSH_AUTHORITY':
console.log('Push service status: ', message.data);
break;
}
});
Return Result
{
"mtPush": {
"code": 1, // 1: Success, -1: Initializing, 0: Failure
"msg": "MTpush channel initialization successful!"
},
"webPush": {
"code": 1, // 0: Webpush unavailable (unsupported), 1: Available, 2: Unavailable
"msg": "Notification available"
}
}
Set Tags and Alias
Interface Description
Developers can use this interface to set and delete tags under a specific alias. This interface uses an overwrite logic. Setting an empty string for tags will delete existing tags.
Example Call
window.MTpushInterfaceExtension.setTagsAlias({ tags: ["test1", "test2"], alias: "aliass" });
Parameter Description
Parameter | Type | Description |
---|---|---|
tags | string[] | Required. Maximum array length is 1000. Each element can be up to 40 characters. |
alias | string | Required. Maximum 40 characters. |
Return Result
Listen for messages to get the result of the interface setting:
chrome.runtime.onMessage.addListener((message) => {
switch (message.type) {
case 'MTPUSH_SET_TAGS_ALIAS_SUCCESS':
console.log('Setting successful');
break;
case 'MTPUSH_SET_TAGS_ALIAS_FAIL':
console.log('Setting failed', message.data); // Failure information: string[]
break;
}
});
Custom Message Reporting
Interface Description
If you need to report custom messages for data statistics, use this custom reporting interface.
Example Call
window.MTpushInterfaceExtension.customClickReport('msg_id'); // msg_id is the ID of the custom message
Error Codes
Code | Message | Remarks |
---|---|---|
0 | success | Call successful |
1000 | unknown error | Unknown error |
1001 | initing , please try again later | Initializing, please try again later |
1002 | invalid config | Invalid initialization configuration |
1003 | init failed | Initialization failed. Check console logs for details. |
1004 | init timeout | Initialization timed out |
1005 | network error | Network error. No network or unable to connect to WebSocket |