美文网首页
SpringCloud 利用Feign完成Google FCM完

SpringCloud 利用Feign完成Google FCM完

作者: 假程序猿 | 来源:发表于2019-10-20 15:30 被阅读0次

    FCM消息实体:

    import java.util.Map;
    
    @lombok.Data
    @lombok.ToString
    public class FcmSendReqVo {
    
        private Notification notification = new Notification();
        private Map<String, Object> data;
        private String to;
    
        public FcmSendReqVo(String token, String title, Map<String, Object> data) {
            this.to = token;
            this.notification.setTitle(title);
            this.data = data;
        }
    
        @lombok.Data
        private class Notification {
            private String title;
            private String body;
            private String sound = "default";
        }
    
    }
    

    Feign接口:

    import com.nboot.push.feign.vo.FcmSendReqVo;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @FeignClient(name = "google-fcm", url = "https://fcm.googleapis.com")
    @RequestMapping("/fcm")
    public interface GoogleFcmFeign {
    
        /**
         * 发送
         * @param apiKey
         * @param reqVo
         * @return
         */
        @PostMapping("/send")
        String send(@RequestHeader("Authorization") String apiKey,  @RequestBody FcmSendReqVo reqVo);
    
    }
    

    调用:

    // 谷歌推送token,需要APP端上传
    String token = 'xxxxxxxxxx';
    // 消息内容
    String msg = "这是一条安卓推送消息";
    // 组装data,自定义数据
    Map<String, Object> data = new HashMap<>(8);
    data.put("id", 123456);
    data.put("type", 1);
    // 执行推送
    try {
        FcmSendReqVo fcmSendReqVo = new FcmSendReqVo(token, msg, data);
        log.info("Android推送开始: {}", JSON.toJSONString(fcmSendReqVo));
        String result = googleFcmFeign.send("key=" + androidApiKey, fcmSendReqVo);
        JSONObject json = JSON.parseObject(result);
        log.info("Android推送完成:success={}, failure={}, response={}", json.getString("success"), json.getString("failure"), result);
    } catch (Throwable e) {
        log.error("Android推送异常", e);
    }
    

    相关文章

      网友评论

          本文标题:SpringCloud 利用Feign完成Google FCM完

          本文链接:https://www.haomeiwen.com/subject/qqfjuctx.html