HarmonyOS 鸿蒙Next中如何将服务器的进程状态实时推送到手机上?

HarmonyOS 鸿蒙Next中如何将服务器的进程状态实时推送到手机上? 如题,用的是华为的手机,鸿蒙5.1

我想在服务器上运行一个推送服务,

具体就是检测进程运行状态,

检测日志运行状态,

这部分我可以用C++去检测,

但是我想把结果推到手机上,目前是用邮箱去推感觉十分麻烦。

想做成一个app可以通过通知栏去推。 那我手机端应该怎么写个APP可以实现这样功能?有demo吗?

6 回复

更多关于HarmonyOS 鸿蒙Next中如何将服务器的进程状态实时推送到手机上?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


背景知识:

楼主想要实现的是:在手机app上实现一个可以监听服务器运营状态的信息app。如果服务器出现异常,就需要推送一个消息通知手机的app吗?

问题解决:

方法一:

可以使用push Kit来接收服务器的推送信息。

第一步:申请推送消息通道:

开通推送服务

第二步:获取push token信息:

import { pushService } from '@kit.PushKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit';


export default class EntryAbility extends UIAbility {
  // 入参 want 与 launchParam 并未使用,为初始化项目时自带参数
  async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): Promise<void> {
    // 获取Push Token
    try {
      // 获取到的pushtoken发送到服务器
      const pushToken: string = await pushService.getToken();
      hilog.info(0x0000, 'testTag', 'Succeeded in getting push token');
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      hilog.error(0x0000, 'testTag', 'Failed to get push token: %{public}d %{public}s', e.code, e.message);
    }
    // 上报Push Token并上报到您的服务端
  }
}

第三步:服务器推送到手机:

服务器端调试推送

方法二:

也可以在在服务器和客户端建立一个长连接(soket),使用长连接的消息通讯实现。

注意:此方案需要app和服务器一直建立socket通道,或者可以在需要的使用建立通道通讯,关闭时可以使用推送发送消息。

参考:Push Kit简介

实现步骤

1/鸿蒙APP示例:

// 初始化推送服务并获取设备Token

import { getInstance } from '@kit.PushKit';

let pushManager = getInstance();

pushManager.getToken().then(token => {

    console.log('设备Token:', token); // 需将Token传回服务器

});

// 接收推送并展示通知

import { NotificationManager, NotificationRequest } from '@kit.NotificationManager';

class PushReceiver {

  onPushMessageReceive(data: string) {

    let notification: NotificationRequest = {

      content: {

        title: "进程状态告警",

        text: data,

        contentType: Notification.ContentType.NOTIFICATION_TEXT

      }

    };

    NotificationManager.publish(notification);

  }

}

2服务器推送实现

POST /v1/{project_id}/messages HTTP/1.1

Authorization: Bearer {access_token}

Content-Type: application/json

{

  "message": {

    "notification": {

      "title": "服务异常",

      "body": "检测到nginx进程停止"

    },

    "token": ["客户端获取的设备Token"]

  }

}

HarmonyOS Next中可通过分布式能力实现进程状态推送。使用@ohos.distributedDeviceManager模块发现设备,建立安全通道后,通过@ohos.rpc实现跨设备通信。服务端调用分布式数据管理接口发布状态变更,手机端通过订阅机制实时接收更新。需确保设备在同一分布式网络中并完成权限认证。

在HarmonyOS Next中实现服务器进程状态实时推送,可以通过以下方案实现:

1. 服务端实现

  • 使用C++检测进程状态和日志状态
  • 集成华为Push Kit服务端SDK(支持HTTP/RESTful API)
  • 当检测到状态变化时,通过Push Kit向指定设备发送数据消息

2. 手机端App开发

  • 集成Push Kit客户端SDK
  • 在App中实现PushReceiver类处理服务端推送
  • 使用NotificationRequest构建状态通知
  • 关键代码示例:
import push from '@ohos.push';
import notificationManager from '@ohos.notificationManager';

// 注册Push回调
push.on('pushReceive', (data) => {
  // 解析服务器发送的状态信息
  let processStatus = JSON.parse(data.content);
  
  // 创建状态通知
  let notificationRequest: notificationManager.NotificationRequest = {
    content: {
      contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
      normal: {
        title: '进程状态更新',
        text: `进程: ${processStatus.name} 状态: ${processStatus.status}`,
        // 其他通知参数...
      }
    }
  };
  
  notificationManager.publish(notificationRequest);
});

3. 实时性保障

  • 配置Push Kit为高优先级消息
  • 服务端检测到状态变化立即触发推送
  • 手机端无需保持App活跃,系统级推送通道保障消息到达

4. 参考资源

  • 华为开发者联盟Push Kit开发文档
  • DevEco Studio中的Push Kit示例模板
  • 分布式数据管理相关API用于状态同步

这种方式比邮件推送更实时高效,且无需App常驻后台,通过系统级推送通道实现状态实时通知。

回到顶部