1/* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License") 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import Extension from '@ohos.app.ability.ServiceExtensionAbility' 17import Notification from '@ohos.notificationManager' 18import rpc from '@ohos.rpc' 19import Logger from '../util/Logger' 20 21const TAG: string = 'ServiceExtAbility' 22let notificationRequest = { 23 content: { 24 contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 25 normal: { 26 title: 'data insert', 27 text: '' 28 } 29 }, 30 id: 1 //ID of the notification request 31} 32 33class FirstServiceAbilityStub extends rpc.RemoteObject { 34 constructor(des: any) { 35 if (typeof des === 'string') { 36 super(des) 37 } else { 38 return 39 } 40 } 41 42 onRemoteRequest(code: number, data: any, reply: any, option: any) { 43 Logger.info(TAG, `onRemoteRequest calledt`) 44 if (code === 1) { 45 let result = data.readString() 46 Logger.info(TAG, `result=${result}`) 47 reply.writeString(result) 48 } else { 49 Logger.info(TAG, `unknown request code`) 50 } 51 return true 52 } 53} 54 55export default class ServiceExtAbility extends Extension { 56 onCreate(want) { 57 Logger.info(TAG, `onCreate, want: ${want.abilityName}`) 58 Logger.info(TAG, `parameters, want: ${want.parameters}`) 59 } 60 61 onRequest(want, startId) { 62 Logger.info(TAG, `onRequest, want: ${want.abilityName}`) 63 notificationRequest.content.normal.text = want.parameters.isInsert 64 Notification.publish(notificationRequest).then(() => { 65 Logger.info(TAG, `publishCallback success`) 66 }) 67 } 68 //用于和客户端进行通信 69 onConnect(want) { 70 Logger.info(TAG, `onConnect , want: ${want.abilityName}`) 71 return new FirstServiceAbilityStub('first ts service stub') 72 } 73 //当新客户端在所有以前的客户端连接之后尝试连接到服务扩展时调用 74 onReconnect(want) { 75 Logger.info(TAG, `onReconnect, want: ${want.abilityName}`) 76 } 77 //断开服务连接时回调 78 onDisconnect(want) { 79 Logger.info(TAG, `onDisconnect, want: ${want.abilityName}`) 80 } 81 82 onDestroy() { 83 Logger.info(TAG, `onDestroy`) 84 } 85}