Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.
initializeApp
Description: Initializes the object of an app integrated with HUAWEI HMS Core.
Parameters:
Options: HmsOptions
name: string (optional)
Sample code:
- hms.initializeApp(hmsConfig);
Description: Obtains a messaging object of the WebPush agent for operations such as obtaining tokens.
Parameters: none
Return: an HmsMessaging object
Process: 1. The messaging method checks whether initialization is performed. If not, the method reports an error. 2. The method checks whether the current environment is a browser or ServiceWorker environment and whether the environment supports the API capability required by the SDK. If not, the method reports an error. 3. The method returns a ServiceWorker messaging or windows messaging object based on the environment.
Description: Obtains the token of the WebPush agent. The token can be used to send messages to the user.
Parameters: none
Return: Promise<string | null> (the value of string is a token)
Sample code:
- messaging.getToken().then((currentToken) => {
- if (currentToken) {
- console.log('Current token: ', currentToken);
- } else {
- console.log('No Instance ID token available. Request permission to generate one.');
- }
- }).catch((err) => {
- console.log('An error occurred while retrieving token. ', err);
- });
Description: Deletes a registered token to forcibly stop using the token.
Parameters: none
Return: Promise + success flag
Sample code:
- messaging.getToken().then((currentToken) => {
- console.log('Current token: ', currentToken);
- return messaging.deleteToken(currentToken);
- }).then((success) => {
- console.log('Delete token result: ', success);
- });
Description: Initializes PublicVapidKey.
Parameters: publicKey (string)
Return: none
Sample code:
- messaging.usePublicVapidKey("your api key");
Description: Registers a customized ServiceWorker. If no customized ServiceWorker is available, the system uses hw-messaging-sw.js in the root path to register the ServiceWorker process by default.
Parameters: registration (Set this parameter to ServiceWorkerRegistration.)
Return: none
Sample code:
- navigator.serviceWorker.register("hms-messaging-sw.js", {
- scope: "./hms-cloud-messaging-push-scope"
- }).then((registration) => {
- messaging.useServiceWorker(registration);
- })
Description: Transfers a message to the page of the message source and uses the callback function to process the message when receiving the message and detecting that the user is on the page of the message source.
Parameters:
nextOrObserver: NextFn | Observer,
error?: ErrorFn (reserved parameter)
completed?: CompleteFn (reserved parameter)
Return: none
Process
The ServiceWorker process receives a message and forwards it to the Windows operating system. The Windows operating system determines whether the message is sent by HUAWEI HMS Core in the background. If so, the Windows operating system calls the NextFn function by using message as the input parameter.
Sample code:
- messaging.onMessage((payload) => {
- console.log('Message received. ', payload);
- });
Description: Sets a callback function, which will be used to process a message when it is found that the message received only contains data load information and the user is offline on the page of the message source.
Parameters: callback (Set this parameter to BgMessageHandler. callback is used after the ServiceWorker process receives a customized message. You can customize the native notification.)
Return: none
Sample code:
- messaging.setBackgroundMessageHandler(function (payload) {
- console.log('[hms-messaging-sw.js] Received background message ', payload);
- const notificationTitle = 'Background Message Title';
- const notificationOptions = {
- body: 'Background Message body.',
- icon: '/hms-logo.png'
- };
- return self.registration.showNotification(notificationTitle,
- notificationOptions);
- });
Description: Registers the ServiceWorker process by default.
Sample code:
- importScripts('https://push-static.dbankcdn.com/hms-messaging.js');
- // Your web app's hmsConfig configuration
- var hmsConfig = {
- apiKey: "your apiKey",
- appId: "your appId"
- };
- // Initialize hms
- hms.initializeApp(hmsConfig);
- const messaging = hms.messaging();
- messaging.setBackgroundMessageHandler(function (payload) {
- console.log('[hms-messaging-sw.js] Received background message ', payload);
- // Customize notification here
- const notificationTitle = 'Background Message Title';
- const notificationOptions = {
- body: 'Background Message body.',
- icon: '/hms-logo.png'
- };
- return self.registration.showNotification(notificationTitle,
- notificationOptions);
- });