1/* 2 * Copyright (c) 2022 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 Notification from '@ohos.notification' 17import rpc from '@ohos.rpc' 18import Logger from '../MainAbility/util/Logger' 19 20const TAG: string = 'service' 21let notificationRequest = { 22 content: { 23 contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 24 normal: { 25 title: 'start service', 26 text: '' 27 } 28 }, 29 id: 1 30} 31 32class FirstServiceAbilityStub extends rpc.RemoteObject { 33 constructor(des: any) { 34 if (typeof des === 'string') { 35 super(des) 36 } else { 37 return 38 } 39 } 40 41 // 处理客户端传入的数据 42 onRemoteRequest(code: number, data: any, reply: any, option: any) { 43 Logger.info(TAG, `onRemoteRequest called`) 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 55// 对应stage模型中 ServiceExtAbility.ts 56export default { 57 onStart() { 58 Logger.info(TAG, 'ServiceAbility onStart') 59 }, 60 onStop() { 61 Logger.info(TAG, 'ServiceAbility onStop') 62 }, 63 onCommand(want, startId) { 64 Logger.info(TAG, `ServiceAbility onCommand, want = ${JSON.stringify(want)}`) 65 notificationRequest.content.normal.text = want.parameters.service 66 Notification.publish(notificationRequest).then(() => { 67 Logger.info(TAG, `publishCallback success`) 68 }) 69 }, 70 onConnect(want) { 71 Logger.info(TAG, `onConnect, want:${JSON.stringify(want)}`) 72 return new FirstServiceAbilityStub('first ts service stub') 73 }, 74 onDisconnect() { 75 Logger.info(TAG, `onDisconnect`) 76 } 77}