jS 消息推送通知 goEasy

开发过程中,需要实现一个实时推送消息的功能,当进行某个操作的时候,实时地发送一条信息给固定角色的人员,查了下资料,这里有一个第三方的免费推送服务goEasy,下面来说下简单的代码实现:

1、在goEasy官网上(http://goeasy.io/)注册一个用户,生成一个application,获取一个super Key,Subscribe key,需要注意的是,super Key可用于推送消息,也可用于接收消息,而Subscribe key只能用于接收消息,在这里,我统一使用的都是super Key。

        2、在需要实现推送消息与接收消息的页面引入goEasy的包:<script type="text/javascript" src="https://cdn.goeasy.io/goeasy.js"></script>

        3、在需要推送消息的界面,加入以下代码:

注意 message是你要传给接收时的信息

      4、在需要接收推送消息的js里,加上如下代码

完成

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Goeasy Webhook同步历史消息的Java案例: 1. 首先,您需要为Goeasy频道启用Webhook。在Goeasy官网上创建一个新的Webhook,并将其指向您的服务器URL: ```java String webhookUrl = "您的服务器URL"; String channelName = "您的频道名称"; String appKey = "您的App Key"; Goeasy goEasy = new Goeasy("您的App Key"); goEasy.createWebhook(channelName, webhookUrl); ``` 2. 在您的服务器上,您需要创建一个接收Goeasy Webhook的端点。在此端点中,您需要解析JSON请求并将其添加到聊天记录中。对于历史消息,您需要将其添加到另一个数据结构中,并在应用程序中使用它。 ```java @RestController @RequestMapping("/webhook") public class WebhookController { private final List<String> chatHistory = new ArrayList<>(); private final List<String> historyMessages = new ArrayList<>(); @PostMapping("/{channelName}") public void handleWebhook(@PathVariable String channelName, @RequestBody String payload) { // 解析JSON请求 JSONObject jsonObject = JSON.parseObject(payload); String content = jsonObject.getString("content"); String publishTime = jsonObject.getString("publishTime"); String eventType = jsonObject.getString("eventType"); // 将消息添加到聊天记录或历史消息 if (eventType.equals("PUBLISH")) { chatHistory.add(content); } else if (eventType.equals("HISTORY_MESSAGE")) { historyMessages.add(content); } } @GetMapping("/{channelName}") public List<String> getChatHistory(@PathVariable String channelName) { return chatHistory; } @GetMapping("/{channelName}/history") public List<String> getHistoryMessages(@PathVariable String channelName) { return historyMessages; } } ``` 3. 在您的应用程序中,您可以使用HTTP客户端从您的服务器获取历史消息。您可以在应用程序中使用此历史消息来显示聊天历史记录。 ```java String webhookUrl = "您的服务器URL"; String channelName = "您的频道名称"; // 获取历史消息 List<String> historyMessages = new ArrayList<>(); RestTemplate restTemplate = new RestTemplate(); String url = webhookUrl + "/" + channelName + "/history"; ResponseEntity<List> response = restTemplate.getForEntity(url, List.class); historyMessages.addAll(response.getBody()); ``` 这样,您就可以使用Goeasy Webhook同步历史消息了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值