initializeApp
描述:初始化hms应用对象。
参数:
Options: HmsOptions
name: string 选填
示例代码:
- hms.initializeApp(hmsConfig);
描述:获取WebPush的messaging对象,用于获取token等操作。
参数:无
返回:HmsMessaging对象
流程:1、校验是否进行过初始化,否抛错;2、判断当前环境是否是浏览器前端或者ServiceWorker环境,且环境是否支持SDK所需要的API能力,否抛错;3、根据环境返回ServiceWorker messaging对象或者windows messaging对象
描述:可以调用此方法以获取WebPush的token,该token可用于向该用户发送推送消息。
参数:无
返回:Promise<string | null> :string为token
示例代码:
- 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);
- });
描述:要强制停止使用注册的token,请通过调用此方法删除token。
参数:无
返回:Promise 成功标识
示例代码:
- messaging.getToken().then((currentToken) => {
- console.log('Current token: ', currentToken);
- return messaging.deleteToken(currentToken);
- }).then((success) => {
- console.log('Delete token result: ', success);
- });
描述:初始化PublicVapidKey
参数:publicKey:string
返回:无
示例代码:
- messaging.usePublicVapidKey("your api key");
描述:利用此方法可以注册自定义的ServiceWorker;如果没有自定义的ServiceWorker,系统会默认使用根路径下的hw-messaging-sw.js来注册ServiceWorker进程
参数:registration: ServiceWorkerRegistration
返回:无
示例代码:
- navigator.serviceWorker.register("hms-messaging-sw.js", {
- scope: "./hms-cloud-messaging-push-scope"
- }).then((registration) => {
- messaging.useServiceWorker(registration);
- })
描述:当接收到推送消息并且用户正在消息来源的页面上时,该消息将传递到页面,并使用回调函数处理推送的消息。
参数:
nextOrObserver: NextFn | Observer,
error?: ErrorFn,暂时没有使用
completed?: CompleteFn,暂时没有使用
返回:无
流程
SW接到push消息,推送到windows。windows判断是否为后台hms消息推送,将messgae作为入参调用NextFn函数。
示例代码:
- messaging.onMessage((payload) => {
- console.log('Message received. ', payload);
- });
描述:在接收到推送消息时,用户未在消息来源的页面上,并且只有data负载的消息,将调用setBackgroundMessageHandler()传递的回调方法,处理消息。
参数: callback: BgMessageHandler,callback在ServiceWorker接收到定制消息后调用,可支持业务对原生Notification定制
返回:无
示例代码:
- 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);
- });
描述:默认注册ServiceWorker进程
示例代码:
- 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);
- });