友盟推送集成

在友盟官网注册应用,获得appkey,secret等信息。

 

下载sdk,

主lib导入jar包,

umeng-common-1.5.3.jar

utdid4all-1.1.5.3_proguard.jar

导入module   push 

再settings.gradle :

include ':app', ':push'

创建MyApplication,进行初始化:

 private void initUM() {
/**
 * 初始化common库
 * 参数1:上下文,不能为空
 * 参数2:友盟 app key
 * 参数3:友盟 channel
 * 参数4:设备类型,UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子,默认为手机
 * 参数5:Push推送业务的secret   todo Umeng Message Secret
 */
        UMConfigure.init(this, "APPKEY",
                "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "SECRET");
        PushAgent mPushAgent = PushAgent.getInstance(this);
      //注册推送服务,每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
  
            }

            @Override
            public void onFailure(String s, String s1) {
           
            }
        });

        mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
    }

注册成功会返回devicetoken

 

然后进行推送。

接收消息

再Manifest中Appliciation节点中注册

<!-- 请填写实际的类名,下面仅是示例代码-->
<service android:name="MyPushIntentService" />
public class MyPushIntentService extends UmengMessageService {


    private static final String TAG = MyPushIntentService.class.getName();

    @Override
    public void onMessage(Context context, Intent intent) {
        try {
            //可以通过MESSAGE_BODY取得消息体
            String message = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
            UMessage msg = new UMessage(new JSONObject(message));
            MLog.d(TAG, "message=" + message);      //消息体
            MLog.d(TAG, "custom=" + msg.custom);    //自定义消息的内容
            MLog.d(TAG, "title=" + msg.title);      //通知标题
            MLog.d(TAG, "text=" + msg.text);        //通知内容
            // code  to handle message here
            // ...

            // 对完全自定义消息的处理方式,点击或者忽略
            boolean isClickOrDismissed = true;
            if (isClickOrDismissed) {
                //完全自定义消息的点击统计
                UTrack.getInstance(getApplicationContext()).trackMsgClick(msg);
            } else {
                //完全自定义消息的忽略统计
                UTrack.getInstance(getApplicationContext()).trackMsgDismissed(msg);
            }
/*
            // 使用完全自定义消息来开启应用服务进程的示例代码
            // 首先需要设置完全自定义消息处理方式
            // mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
            // code to handle to start/stop service for app process
            JSONObject json = new JSONObject(msg.custom);
            String topic = json.getString("topic");
            MLog.d(TAG, "topic=" + topic);
            if (topic != null && topic.equals("appName:startService")) {
                // 在【友盟+】portal上新建自定义消息,自定义消息文本如下
                //{"topic":"appName:startService"}
                if (Helper.isServiceRunning(context, NotificationService.class.getName()))
                    return;
                Intent intent1 = new Intent();
                intent1.setClass(context, NotificationService.class);
                context.startService(intent1);
            } else if (topic != null && topic.equals("appName:stopService")) {
                // 在【友盟+】portal上新建自定义消息,自定义消息文本如下
                //{"topic":"appName:stopService"}
                if (!Helper.isServiceRunning(context,NotificationService.class.getName()))
                    return;
                Intent intent1 = new Intent();
                intent1.setClass(context, NotificationService.class);
                context.stopService(intent1);
            }*/
        } catch (Exception e) {
            MLog.e(TAG, e.getMessage());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
友盟推送集成Spring Boot可以通过友盟提供的Java SDK来实现。具体步骤如下: 1.在pom.xml文件中添加友盟推送的Java SDK依赖: ```xml <dependency> <groupId>com.umeng</groupId> <artifactId>umeng-message</artifactId> <version>1.3.2</version> </dependency> ``` 2.在application.properties文件中添加友盟推送的配置信息: ```properties # 友盟推送配置 umeng.appkey=your_appkey umeng.appMasterSecret=your_app_master_secret umeng.productionMode=false ``` 3.编写友盟推送的Java代码: ```java import com.alibaba.fastjson.JSONObject; import com.umeng.message.*; import com.umeng.message.common.inter.ITagManager; import com.umeng.message.entity.UMessage; import com.umeng.message.tag.TagManager; import java.util.List; public class UmengPushService implements UmengNotificationService { private final AndroidNotification androidNotification; private final IOSNotification iosNotification; private final PushClient client; public UmengPushService(String appKey, String appMasterSecret, boolean productionMode) { androidNotification = new AndroidNotification(); iosNotification = new IOSNotification(); client = new PushClient(); client.setAppKey(appKey); client.setAppMasterSecret(appMasterSecret); client.setProductionMode(productionMode); } @Override public void sendUnicast(String deviceToken, String title, String text, JSONObject extra) throws Exception { UMessage message = new UMessage(); message.setDeviceToken(deviceToken); message.setTitle(title); message.setText(text); message.setExtra(extra); androidNotification.setAlert(text); iosNotification.setAlert(text); message.setNotificationAndroid(androidNotification); message.setNotificationIOS(iosNotification); client.send(message); } @Override public void sendListcast(List<String> deviceTokens, String title, String text, JSONObject extra) throws Exception { UMessage message = new UMessage(); message.setDeviceToken(deviceTokens); message.setTitle(title); message.setText(text); message.setExtra(extra); androidNotification.setAlert(text); iosNotification.setAlert(text); message.setNotificationAndroid(androidNotification); message.setNotificationIOS(iosNotification); client.send(message); } @Override public void sendBroadcast(String title, String text, JSONObject extra) throws Exception { UMessage message = new UMessage(); message.setTitle(title); message.setText(text); message.setExtra(extra); androidNotification.setAlert(text); iosNotification.setAlert(text); message.setNotificationAndroid(androidNotification); message.setNotificationIOS(iosNotification); client.send(message); } @Override public void addTag(String deviceToken, String tag) throws Exception { ITagManager tagManager = new TagManager(); tagManager.add(deviceToken, tag); } @Override public void deleteTag(String deviceToken, String tag) throws Exception { ITagManager tagManager = new TagManager(); tagManager.delete(deviceToken, tag); } } ``` 4.在Spring Boot中使用友盟推送: ```java @RestController @RequestMapping("/push") public class PushController { @Autowired private UmengNotificationService umengNotificationService; @PostMapping("/unicast") public void unicast(@RequestParam String deviceToken, @RequestParam String title, @RequestParam String text, @RequestParam JSONObject extra) throws Exception { umengNotificationService.sendUnicast(deviceToken, title, text, extra); } @PostMapping("/listcast") public void listcast(@RequestParam List<String> deviceTokens, @RequestParam String title, @RequestParam String text, @RequestParam JSONObject extra) throws Exception { umengNotificationService.sendListcast(deviceTokens, title, text, extra); } @PostMapping("/broadcast") public void broadcast(@RequestParam String title, @RequestParam String text, @RequestParam JSONObject extra) throws Exception { umengNotificationService.sendBroadcast(title, text, extra); } @PostMapping("/addTag") public void addTag(@RequestParam String deviceToken, @RequestParam String tag) throws Exception { umengNotificationService.addTag(deviceToken, tag); } @PostMapping("/deleteTag") public void deleteTag(@RequestParam String deviceToken, @RequestParam String tag) throws Exception { umengNotificationService.deleteTag(deviceToken, tag); } } ``` 以上代码演示了如何在Spring Boot中使用友盟推送,包括单播、列播、广播和添加/删除标签等操作。需要注意的是,以上代码仅供参考,具体实现需要根据自己的业务需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值