栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java集成极光或MobPush推送

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java集成极光或MobPush推送

极光

极光推送官网地址: 极光-android消息推送-ios消息推送-app消息推送-手机移动推送服务平台SDK快速集成

官方文档地址:极光推送 - REST API 概述 - 极光文档

1、注册极光账号创建应用获取到appKey和masterSecret

2、导入依赖

 

    cn.jpush.api
    jpush-client
    3.2.3
   

3、创建工具类demo

package com.tanshu.project.tools.push;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import java.util.HashMap;

public class JiGuangPushUtil {

    
    public static void pushAllAndroid(String type, String userId[], String message,String title, String appKey, String masterSecret) {
        //两个参数分别填写你申请的masterSecret和appKey
        JPushClient jPushClient = new JPushClient(masterSecret, appKey);
        PushPayload.Builder builder = PushPayload.newBuilder();
        builder.setPlatform(Platform.android());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
        //设置如果用户不在线、离线消息保存的时间
        Options options = Options.sendno();
        options.setTimeToLive(86400L);    //设置为86400为保存一天,如果不设置默认也是保存一天
        builder.setOptions(options);
        //设置推送方式
        if ("alias".equals(type)) {
            builder.setAudience(Audience.alias(userId));//根据别名推送
        } else if ("tag".equals(type)) {
            builder.setAudience(Audience.tag(userId));//根据标签推送
        } else {
            builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
        }
        //设置为采用通知的方式发送消息
//        builder.setNotification(Notification.alert(alert));
        builder.setNotification(Notification.android(message, title, new HashMap<>()));
        PushPayload pushPayload = builder.build();
        try {
            //进行推送,实际推送就在这一步
            PushResult pushResult = jPushClient.sendPush(pushPayload);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    public static void pushAllIOSDev(String type, String[] userId, String message, String appKey, String masterSecret) {
        //两个参数分别填写你申请的masterSecret和appKey
        JPushClient jPushClient = new JPushClient(masterSecret, appKey);
        PushPayload.Builder builder = PushPayload.newBuilder();
        builder.setPlatform(Platform.ios());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
        //设置如果用户不在线、离线消息保存的时间
        Options options = Options.sendno();
        options.setTimeToLive(86400l);    //设置为86400为保存一天,如果不设置默认也是保存一天
        options.setApnsProduction(false);
        builder.setOptions(options);
        //设置推送方式
        if ("alias".equals(type)) {
            builder.setAudience(Audience.alias(userId));//根据别名推送
        } else if ("tag".equals(type)) {
            builder.setAudience(Audience.tag(userId));//根据标签推送
        } else {
            builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
        }
        //设置为采用通知的方式发送消息
        builder.setNotification(Notification.alert(message));
        PushPayload pushPayload = builder.build();
        try {
            //进行推送,实际推送就在这一步
            PushResult pushResult = jPushClient.sendPush(pushPayload);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    public static void pushAllIOSDis(String type, String[] userId, String message, String appKey, String masterSecret) {
        //两个参数分别填写你申请的masterSecret和appKey
        JPushClient jPushClient = new JPushClient(masterSecret, appKey);
        PushPayload.Builder builder = PushPayload.newBuilder();
        builder.setPlatform(Platform.ios());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
        //设置如果用户不在线、离线消息保存的时间
        Options options = Options.sendno();
        options.setTimeToLive(86400l);    //设置为86400为保存一天,如果不设置默认也是保存一天
        options.setApnsProduction(true);
        builder.setOptions(options);
        //设置推送方式
        if ("alias".equals(type)) {
            builder.setAudience(Audience.alias(userId));//根据别名推送
        } else if ("tag".equals(type)) {
            builder.setAudience(Audience.tag(userId));//根据标签推送
        } else {
            builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
        }
        //设置为采用通知的方式发送消息
        builder.setNotification(Notification.alert(message));
        PushPayload pushPayload = builder.build();
        try {
            //进行推送,实际推送就在这一步
            PushResult pushResult = jPushClient.sendPush(pushPayload);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    public static void pushAll(String type, String[] userId, String message, String title, String appKey, String masterSecret) {
        //两个参数分别填写你申请的masterSecret和appKey
        JPushClient jPushClient = new JPushClient(masterSecret, appKey);
        PushPayload.Builder builder = PushPayload.newBuilder();
        builder.setPlatform(Platform.all());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
        //设置如果用户不在线、离线消息保存的时间
        Options options = Options.sendno();
        options.setTimeToLive(86400l);    //设置为86400为保存一天,如果不设置默认也是保存一天
        builder.setOptions(options);
        //设置推送方式
        if ("alias".equals(type)) {
            builder.setAudience(Audience.alias(userId));//根据别名推送
        } else if ("tag".equals(type)) {
            builder.setAudience(Audience.tag(userId));//根据标签推送
        } else {
            builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
        }
        //设置为采用通知的方式发送消息
        builder.setNotification(Notification.alert(message));
        builder.setNotification(Notification.android(message, title, new HashMap<>()));
        PushPayload pushPayload = builder.build();
        try {
            //进行推送,实际推送就在这一步
            PushResult pushResult = jPushClient.sendPush(pushPayload);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	//测试类
    public static void main(String[] args) {
        String[] str = {"PROdati-9197-user"};
        pushAllAndroid("alias", str, "你有新的任务,请及时处理呀", "提醒","appKey", "masterSecret");
    }
}
MobPush

MOB官网地址:MobPush-专业免费推送SDK,智能化推送服务-MobTech

MobPush官方文档地址:MobTech集成文档-MobTech

1、注册MOB账号创建应用获取到App Key和App Secret

集成Android各个平台的信息

集成IOS需要的信息

这就是ios的证书

2、导入依赖

  
  
      com.mob.push.sdk
      mobpush-websdkv3-java
      2.0.3
  

3、创建工具类

package com.tanshu.project.tools.push;
​
import com.tanshu.project.tools.DataUtils;
import lombok.extern.slf4j.Slf4j;
import mob.push.api.builder.PushWorkBuilder;
import mob.push.api.client.push.PushV3Client;
import mob.push.api.config.MobPushConfig;
import mob.push.api.exception.ApiException;
import mob.push.api.http.Result;
import mob.push.api.model.Push;
import mob.push.api.res.PushV3Res;
import mob.push.api.utils.SetUtil;
​
import java.util.UUID;
​
@Slf4j
public class MobPushUtils {
    
    private static Result pushAllAndroid(String workNo, String[] userID, String title, String content) {
        Push push = (new PushWorkBuilder()).setTargetAll(workNo, title, content).build();
        push.getPushNotify().setPlats(SetUtil.newSet(new Integer[]{1}));
        if (userID.length > 0) {
            //push.getSource().
            push.getPushTarget().setTarget(2);
            push.getPushTarget().setAlias(SetUtil.newSet(userID));
        }
        Result pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }
​
    //发送正式服
    private static Result pushAllIOSDis(String workNo, String[] userID, String title, String content) {
        Push push = (new PushWorkBuilder()).setTargetAll(workNo, title, content).build();
        push.getPushNotify().setIosProduction(Push.IOS_PROD);
        push.getPushNotify().setPlats(SetUtil.newSet(new Integer[]{2}));
        if (userID.length > 0) {
            //push.getSource().
            push.getPushTarget().setTarget(2);
            push.getPushTarget().setAlias(SetUtil.newSet(userID));
        }
        Result pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }
​
    //发送测试服
    private static Result pushAllIOSDev(String workNo, Long[] userID, String title, String content) {
        Push push = (new PushWorkBuilder()).setTargetAll(workNo, title, content).build();
        push.getPushNotify().setIosProduction(Push.IOS_DEV);
        push.getPushNotify().setPlats(SetUtil.newSet(new Integer[]{2}));
        if (userID.length>0) {
            //push.getSource().
            push.getPushTarget().setTarget(2);
            push.getPushTarget().setAlias(SetUtil.newSet(String.valueOf(userID)));
        }
        Result pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }
​
    //发送全部
​
    
    public static void pushAll(String title, String message,String AppKey,String AppSecret) {
​
        MobPushConfig.appkey = AppKey;
        MobPushConfig.appSecret = AppSecret;
​
        try {
            //Registration ID推送
            //Result resResult = PushV3Client.pushByRids("workNo", "title", "content", "rid");
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(uuid);
//            //发送测试
//            pushAllIOSDev(uuid,null,title,message);
//            uuid = UUID.randomUUID().toString().replaceAll("-","");
//            System.out.println(uuid);
            //发送正式
            pushAllIOSDis(uuid,null,title,message);
​
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(uuid);
            //发送Android
            pushAllAndroid(uuid, null, title, message);
​
        } catch (ApiException e) {
            e.getStatus();           //错误请求状态码
            e.getErrorCode();       //错误状态码
            e.getErrorMessage();   //错误信息
        }
​
    }
​
    public static void pushUser(String[] userId, String title, String message,String AppKey,String AppSecret) {
        MobPushConfig.appkey = AppKey;
        MobPushConfig.appSecret = AppSecret;
​
        try {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            System.out.println(uuid);
            //发送测试
//            pushAllIOSDev(uuid,userId,title,message);
//            uuid = UUID.randomUUID().toString().replaceAll("-","");
//            System.out.println(uuid);
            //发送正式
            pushAllIOSDis(uuid,userId,title,message);
​
//            uuid = UUID.randomUUID().toString().replaceAll("-","");
//            System.out.println(uuid);
            //发送Android
            pushAllAndroid(uuid, userId, title, message);
​
        } catch (ApiException e) {
            e.getStatus();           //错误请求状态码
            e.getErrorCode();       //错误状态码
            e.getErrorMessage();   //错误信息
        }
    }
​
    public static void main(String[] args) {
//        pushUser(Long.parseLong("3144"),"java 测试消息", "5月20苹果中","AppKey","AppSecret");
    }
}

 最后自己使用工具类的main方法测试即可

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318142.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号