zoukankan      html  css  js  c++  java
  • App给自己推送消息

    procedure TMainDataModule.SendNotification(const ATitle, AAlertBody, AData: string);
    var
       mNotice: TNotification;
    begin
      if NotificationCenter1.Supported then
      begin
        mNotice := NotificationCenter1.CreateNotification;
        try
          mNotice.Name := kbmMWGlobal.kbmMWGenerateGUID;//这里我用了唯一的名子,让每条消息都是新的,如果用固定的名子,那么新消息来时会覆盖旧的消息,即在消息栏中不会产生新的一条消息。
          mNotice.Title := ATitle;//消息栏中的标题
          mNotice.AlertBody := AAlertBody;//在消息栏中显示的具体内容
          mNotice.HasAction:=True;
          mNotice.AlertAction:=AData;//透传的Json,业务逻辑可以通过Json来定义
          // 通知
          NotificationCenter1.PresentNotification(mNotice);
        finally
          mNotice.DisposeOf;
        end;
      end;
    end;

     上面是一个通用的方法,客户端调用,向app自己发出一个消息,为了接收这个消息,我们还要处理NotificationCenter1的OnReceiveLocalNotification事件:

    procedure TForm10.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
        ANotification: TNotification);
    begin
      //这里处理接收的消息
    end;

    为了更美观,我们还要定义消息的图标:

    上图,定义Notification的图标。

    如果你已经成为DPush的用户,那上面的方法就不用我们去实现了,作者已经封装了上面实现到DPush中,与DPush的处理逻辑合二为一,事情将变的简单!

    首先,通过TLeoDPushService.SendLocalNotification方法来为自己发送消息,下是是我进一步封装到MainDataModule中的方法:

    procedure TMainDataModule.SendLocalNotification(const ATitle, ASubject, AData: string);
    begin
      FPushService.SendLocalNotification(ATitle,ASubject,SO(AData),kbmMWGenerateGUID);
    end;

    其次,接收消息时,自动触发TLeoDPushService.OnGetPushDataEvent事件。与离线消息是一样的,这非常爽了!关于收到离线消息的处理,可以参考前面写的关于DPush的文章。

  • 相关阅读:
    Python集成开发环境Pycharm+Git+Gitee(码云)
    【AI图像识别二】JMeter轻松实现大数据量AI图像识别接口测试
    pycharm中django同步数据库问题
    绿盟-WEB应用漏洞扫描系统
    Python脚本轻松实现批量图片重命名
    Linxu下JMeter进行接口压力测试
    依赖Anaconda环境安装TensorFlow库,避免采坑
    【AI图像识别一】人脸识别测试探索
    Postgresql死锁的处理
    PostgreSQL 9.5,带来 UPSERT 等新特性
  • 原文地址:https://www.cnblogs.com/kinglandsoft/p/14862210.html
Copyright © 2011-2022 走看看