友盟的文档我是真的有点看不懂,没头没尾的,如果不看sdk真的是理解不了,最终还是给拿下了
友盟官方地址:https://developer.umeng.com/docs
废话不多说直接下载sdk:
下载之后集成到项目中(应用配置官网上有可以看一下,这个还是能看懂的,配置一下,android和ios在前端和后端都是两套东西,前端所对应的的需要集成的不同,后端的方法和参数不同)
方法集中在红框框这个工具类里边
推送的一个关键就是友盟的一个deviceToken(后端进行推送的是需要前端传过来deviceToken 随你怎么处理这个玩意,这个token是根据手机和app来的,比如集成后卸载了再下载两个token都是不一样的)
方法的改造性特别强 我这里使用的是一个单播模式所以我只改造了一下两个端的单播方法
如下:
//可以看到不管是那个方法都有正式和测试模式 测试模式只在本地校验你的参数对不对
//正式模式可以在手机上看到推送
public class UmengPushUtil {
private String appkey = "";
private String appMasterSecret = "";
private String timestamp = null;
private PushClient client = new PushClient();
public UmengPushUtil(String key, String secret) {
try {
appkey = key;
appMasterSecret = secret;
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/**
* 推送android 群发消息
* @throws Exception
*/
public void sendAndroidBroadcast() throws Exception {
AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
broadcast.setTicker( "Android broadcast ticker");
broadcast.setTitle( "中文的title");
broadcast.setText( "Android broadcast text");
broadcast.goAppAfterOpen();
broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
// 设置测试模式还是正式模式
broadcast.setProductionMode();
// 设置定制字段
broadcast.setExtraField("test", "helloworld");
//厂商通道相关参数
broadcast.setChannelActivity("your channel activity");
broadcast.setChannelProperties("abc");
client.send(broadcast);
}
/**
* Android 单发消息
* @throws Exception
*/
public void sendAndroidUnicast(String appkey,String appMasterSecret,String deviceToken,Map<String,String> map) throws Exception {
AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
unicast.setDeviceToken(deviceToken);
unicast.setTicker(map.get("ticker"));
unicast.setTitle( map.get("title"));
unicast.setText( map.get("text"));
unicast.goAppAfterOpen();
unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
// 设置测试模式还是正式模式
unicast.setProductionMode();
// 设置定制字段
unicast.setExtraField("test", "helloworld");
unicast.setChannelActivity("your channel activity");
unicast.setChannelProperties("abc");
client.send(unicast);
}
/**
* iso 群发消息
* @throws Exception
*/
public void sendIOSBroadcast() throws Exception {
IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
//alert值设置为字符串
//broadcast.setAlert("IOS 广播测试");
//alert的值设置为字典
broadcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
broadcast.setBadge( 0);
broadcast.setSound( "default");
// 设置测试模式还是
broadcast.setProductionMode();
// 设置定制字段
broadcast.setCustomizedField("test", "helloworld");
client.send(broadcast);
}
/**
* ISO单播
* @throws Exception
*/
public void sendIOSUnicast(String appkey, String appMasterSecret, String deviceToken, Map<String,String> info) throws Exception {
IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
// TODO Set your device token
unicast.setDeviceToken(deviceToken);
//alert值设置为字符串
//unicast.setAlert("IOS 单播测试");
//alert的值设置为字典
unicast.setAlert(info.get("title"),info.get("subtitle"),info.get("body"));
unicast.setBadge( 0);
unicast.setSound( "default");
// 设置测试模式还是正式模式
unicast.setProductionMode();
unicast.setCustomizedField("display_type","notification");
// 设置定制字段
// unicast.setCustomizedField("test", "helloworld");
client.send(unicast);
}
}
集成推送
//此处我把上边的appkey和MasterSecret 存放在nacos中 直接再类里配置也行一个样子
@Value("${umeng.iosAppkey}")
private String iosAppkey ;
@Value("${umeng.iosAppMasterSecret}")
private String iosAppMasterSecret ;
@Value("${umeng.androidAppkey}")
private String androidAppkey;
@Value("${umeng.androidAppMasterSecret}")
private String androidAppMasterSecret ;
@Override
public void run() {
// 定义任务要做的事,完成任务逻辑
UmengPushUtil androidUmengPushUtil = new UmengPushUtil(androidAppkey,androidAppMasterSecret);
UmengPushUtil iosUmengPushUtil = new UmengPushUtil(iosAppkey,iosAppMasterSecret);
//通过任何方式给手机类型取出来
Integer phoneType = XXXXXX.getPhoneType();
//还有最新的devicesToken
String devicesToken = XXXXX.getDevicesToken();
//因为ios和andirod 端不同 所以需要区别手机的类型
if (phoneType==0){//ios
Map<String,String> info = new HashMap<>();
info.put("title","提醒");
info.put("subtitle","提醒");
info.put("body","内容");
try {
iosUmengPushUtil.sendIOSUnicast(iosAppkey,iosAppMasterSecret,devicesToken,info);
log.info("ios 推送成功");
} catch (Exception e) {
e.printStackTrace();
}
}else {//安卓
Map<String,String> info = new HashMap<>();
info.put("ticker","提醒");
info.put("title","醒");
info.put("text","推送");
try {
androidUmengPushUtil.sendAndroidUnicast(androidAppkey,androidAppMasterSecret,devicesToken,info);
log.info("android 推送成功");
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
到这里就完了 可以写一个简单的main方法测试一下测试很容易成功,但是只看文档我是真不想看,有看不懂,另外推送消息不是100%成功的 苹果成功率高一些,安卓各个都有自己的厂商通道,可以查看文档里边的。
文章持续更新有问题可以留言