极光

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

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

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

Java向app分用户推送消息 java app推送_发送消息

2、导入依赖

<!--  极光消息推送  -->
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.3</version>
</dependency>

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 {

    /**
     * Android推送
     * 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
     *
     * @param type         推送方式:tag:标签推送 alias:别名推送
     * @param userId       推送的标签或别名值
     * @param message      推送的内容
     * @param appKey       appKey
     * @param masterSecret masterSecret
     */
    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();
        }

    }

    /**
     * IOS推送开发
     * 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
     *
     * @param type         推送方式:tag:标签推送 alias:别名推送
     * @param userId       推送的标签或别名值
     * @param message      推送的内容
     * @param appKey       appKey
     * @param masterSecret masterSecret
     */
    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();
        }

    }

    /**
     * IOS推送生产
     * 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
     *
     * @param type         推送方式:tag:标签推送 alias:别名推送
     * @param userId       推送的标签或别名值
     * @param message      推送的内容
     * @param appKey       appKey
     * @param masterSecret masterSecret
     */
    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();
        }

    }

    /**
     * 全部推送
     * 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
     *
     * @param type         推送方式:tag:标签推送 alias:别名推送
     * @param userId       推送的标签或别名值
     * @param message      推送的内容
     * @param appKey       appKey
     * @param masterSecret masterSecret
     */
    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需要的信息

Java向app分用户推送消息 java app推送_推送_02

 

Java向app分用户推送消息 java app推送_推送_03

这就是ios的证书

Java向app分用户推送消息 java app推送_Java向app分用户推送消息_04

2、导入依赖

<!--  mobPush消息推送  -->
  <dependency>
      <groupId>com.mob.push.sdk</groupId>
      <artifactId>mobpush-websdkv3-java</artifactId>
      <version>2.0.3</version>
  </dependency>

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 {
    /**
     * 发送消息
     *
     * @param workNo 随便一个UUID
     * @param title 标题
     * @param userID 用户ID(支持多人)
     * @param content 内容
     * @return
     */
    private static Result<PushV3Res> 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<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }

    //发送正式服
    private static Result<PushV3Res> 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<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }

    //发送测试服
    private static Result<PushV3Res> 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<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        if (pushV3ResResult != null) {
            log.info(DataUtils.toJson(pushV3ResResult));
        }
        return pushV3ResResult;
    }

    //发送全部

    /**
     *
     * @param title 标题
     * @param message 内容
     * @param AppKey AppKey
     * @param AppSecret AppSecret
     */
    public static void pushAll(String title, String message,String AppKey,String AppSecret) {

        MobPushConfig.appkey = AppKey;
        MobPushConfig.appSecret = AppSecret;

        try {
            //Registration ID推送
            //Result<PushV3Res> 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方法测试即可