目录

1.hms

initializeApp

描述:初始化hms应用对象。

参数:

Options: HmsOptions

name: string 选填

示例代码:

  1. hms.initializeApp(hmsConfig);

2.messaging

描述:获取WebPush的messaging对象,用于获取token等操作。

参数:无

返回:HmsMessaging对象

流程:1、校验是否进行过初始化,否抛错;2、判断当前环境是否是浏览器前端或者ServiceWorker环境,且环境是否支持SDK所需要的API能力,否抛错;3、根据环境返回ServiceWorker messaging对象或者windows messaging对象

2.1 getToken

描述:可以调用此方法以获取WebPush的token,该token可用于向该用户发送推送消息。

参数:无

返回:Promise<string | null> :string为token

示例代码:

  1. messaging.getToken().then((currentToken) => {
  2.    if (currentToken) {
  3.        console.log('Current token: ', currentToken);
  4.    } else {
  5.        console.log('No Instance ID token available. Request permission to generate one.');
  6.    }
  7. }).catch((err) => {
  8.    console.log('An error occurred while retrieving token. ', err);
  9. });

2.2 deleteToken

描述:要强制停止使用注册的token,请通过调用此方法删除token。

参数:无

返回:Promise 成功标识

示例代码:

  1. messaging.getToken().then((currentToken) => {
  2.    console.log('Current token: ', currentToken);
  3.    return messaging.deleteToken(currentToken);
  4. }).then((success) => {
  5.    console.log('Delete token result: ', success);
  6. });

2.3 usePublicVapidKey

描述:初始化PublicVapidKey

参数:publicKey:string

返回:无

示例代码:

  1. messaging.usePublicVapidKey("your api key");

2.4 useServiceWorker

描述:利用此方法可以注册自定义的ServiceWorker;如果没有自定义的ServiceWorker,系统会默认使用根路径下的hw-messaging-sw.js来注册ServiceWorker进程

参数:registration: ServiceWorkerRegistration

返回:无

示例代码:

  1. navigator.serviceWorker.register("hms-messaging-sw.js", {
  2.    scope: "./hms-cloud-messaging-push-scope"
  3. }).then((registration) => {
  4.    messaging.useServiceWorker(registration);
  5. })

2.5 onMessage

描述:当接收到推送消息并且用户正在消息来源的页面上时,该消息将传递到页面,并使用回调函数处理推送的消息。

参数:

nextOrObserver: NextFn | Observer,

error?: ErrorFn,暂时没有使用

completed?: CompleteFn,暂时没有使用

返回:无

流程

SW接到push消息,推送到windows。windows判断是否为后台hms消息推送,将messgae作为入参调用NextFn函数。

示例代码:

  1. messaging.onMessage((payload) => {
  2.    console.log('Message received. ', payload);
  3. });

2.6 setBackgroundMessageHandler

描述:在接收到推送消息时,用户未在消息来源的页面上,并且只有data负载的消息,将调用setBackgroundMessageHandler()传递的回调方法,处理消息。

参数: callback: BgMessageHandler,callback在ServiceWorker接收到定制消息后调用,可支持业务对原生Notification定制

返回:无

示例代码:

  1. messaging.setBackgroundMessageHandler(function (payload) {
  2.  console.log('[hms-messaging-sw.js] Received background message ', payload);
  3.  const notificationTitle = 'Background Message Title';
  4.  const notificationOptions = {
  5.    body: 'Background Message body.',
  6.    icon: '/hms-logo.png'
  7.  };
  8.  return self.registration.showNotification(notificationTitle,
  9.    notificationOptions);
  10. });

3.hms-messaging-sw.js

描述:默认注册ServiceWorker进程

示例代码:

  1. importScripts('https://push-static.dbankcdn.com/hms-messaging.js');
  2. // Your web app's hmsConfig configuration
  3. var hmsConfig = {
  4.  apiKey: "your apiKey",
  5.  appId: "your appId"
  6. };
  7. // Initialize hms
  8. hms.initializeApp(hmsConfig);
  9. const messaging = hms.messaging();
  10. messaging.setBackgroundMessageHandler(function (payload) {
  11.  console.log('[hms-messaging-sw.js] Received background message ', payload);
  12.  // Customize notification here
  13.  const notificationTitle = 'Background Message Title';
  14.  const notificationOptions = {
  15.    body: 'Background Message body.',
  16.    icon: '/hms-logo.png'
  17.  };
  18.  return self.registration.showNotification(notificationTitle,
  19.    notificationOptions);
  20. });