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);
}
网友评论