极光推送——App推送

极光推送:主要用于APP实时获取最新消息。本文主要描述如何使用极光提供的SDK进行推送。

极光推送中主要需要配置的参数如下:

推送平台:JPush 当前支持 Android, iOS, Windows Phone 三个平台的推送。其关键字分别为:"android", "ios", "winphone"。

推送目标:广播(所有用户)、alias(别名:用别名来标识一个用户)、tag(标签:用标签来进行大规模的设备属性、用户属性分群。 一次推送最多 20 个)等。

推送方式:通知(展示在通知栏)、自定义消息(此部分内容不会展示到通知栏上,JPush SDK 收到消息内容后透传给 App。需要 App 自行处理。应用举例:弹出页面)等。

首先需要在项目的pom文件中引入jar包:

         <dependency>
             <groupId>cn.jpush.api</groupId>
             <artifactId>jpush-client</artifactId>
             <version>3.3.3</version>
         </dependency>

具体应用:

 //声明推送目标  ,在极光平台创建应用,就会生成唯一的AppKey、Master Secret
  
  JPushClient jpushClient = new JPushClient(GlobleConfig.getProperty("JPush_masterSecret"), GlobleConfig.getProperty("JPush_appKey"));
  
  //创建推送对象,举例:根据别名推送通知,具体方法实现在下边的工具类中
  
  PushPayload pushPayload = JpushUtil.buildPushObject_all_aliases_alertWithTitle(alias, "退出登录", "您的账号已在其他设备登录!",map);
 
  //进行推送
 
 try {
      PushResult pushResult = jpushClient.sendPush(pushPayload);                                    
      System.out.println(pushResult+".......");
 } catch (Exception e) {
     //如果推送错误返回的code为1011,就是没有找到推送目标,则需要App配合排查问题
     System.out.println("推送失败");
 }

主要的工具类 JpushUtil.java:

(我设置的推送目标是别名,也可以根据需求改成使用签名等。)

JPushUtil.java 工具类

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.erenju.util.GlobleConfig;    
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xhgx.domain.RestInfo;
import com.xhgx.web.util.JSONObject;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
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.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JpushUtil{
    
    /**
     * 推送通知:以广播形式推送给所有平台
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(){
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//推送平台 :all代表全部 ,也可写具体的平台android或ios
                .setAudience(Audience.all())//推送目标:all表示以广播形式推送,所有用户都可接收到
                .setNotification(Notification.alert("测试广播推送!"))//推送到通知栏的内容
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    
    /**
     * 推送通知:根据alias推送给个人
     * @param alias
     * @param notification_title
     * @param msg_title
     * @param extrasparam
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alertWithTitle(String alias,String notification_title, String msg_title, Map<String,String> extrasparam){
        String iosAlert = notification_title+":"+msg_title; 
        //需要最新sdk版本
//        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
        //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build()
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.alias(alias))
                //通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容
                                .setTitle(notification_title)//标题
                                .setAlert(msg_title)//内容
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等
                                .incrBadge(1)//此项是指定此推送的badge自动加1
                                //.setSound("sound.caf")//设置声音
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .setContentAvailable(true)//是否可以在锁屏状态下接收
                                .build())    
                    .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    /**
     * 推送通知:根据alias同时推送给多个用户
     * @param aliases
     * @param notification_title
     * @param msg_title
     * @param extrasparam
     * @return
     */
    public static PushPayload buildPushObject_all_aliases_alertWithTitle(List<String> aliases,String notification_title, String msg_title, Map<String,String> extrasparam){
        String iosAlert = notification_title+":"+msg_title; 
        //需要最新sdk版本
//        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
        //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build()
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.alias(aliases))
                //通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容
                                .setTitle(notification_title)//标题
                                .setAlert(msg_title)//内容
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等
                                .incrBadge(1)//此项是指定此推送的badge自动加1
                                //.setSound("sound.caf")//设置声音
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .setContentAvailable(true)//是否可以在锁屏状态下接收
                                .build())    
                    .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    
    /**
     * 推送通知和自定义消息:根据alias推给个人
     * @param alias
     * @param notification_title
     * @param msg_title
     * @param msg_content
     * @param extrasparam
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alertAndmessage(String alias,String notification_title,String msg_title,String msg_content,Map<String, String> extrasparam ){
        String iosAlert = notification_title+":"+msg_title;
        //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build()
        
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.alias(alias))
                //通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容
                                .setTitle(notification_title)//标题
                                .setAlert(msg_title)//内容
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、body、subtitle等
                                .incrBadge(1)//此项是指定此推送的badge自动加1
                                //.setSound("sound.caf")//设置声音
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                //.setContentAvailable(true)//是否可以在锁屏状态下接收
                                .build())    
                    .build())
                //自定义消息
                .setMessage(Message.newBuilder()
                        .setTitle(msg_title)//消息标题
                        .setMsgContent(msg_content)//消息内容本身
                        //.setContentType("json")//消息内容类型
                        .addExtras(extrasparam)//json格式的可选参数
                        .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    /**
     * 推送自定义消息:根据alias推给个人
     * @param alias
     * @param msg_title
     * @param msg_content
     * @param extrasparam
     * @return
     */
    public static PushPayload buildPushObject_all_alias_message(String alias,String msg_title,String msg_content,Map<String, String> extrasparam ){
        
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.alias(alias))
                //自定义消息
                .setMessage(Message.newBuilder()
                        .setTitle(msg_title)//消息标题
                        .setMsgContent(msg_content)//消息内容本身
                        //.setContentType("json")//消息内容类型
                        .addExtras(extrasparam)//json格式的可选参数
                        .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    /**
     * 推送通知:根据tag推送给个人
     * @param tag
     * @param tag_add
     * @param notification_title
     * @param msg_title
     * @param extrasparam
     * @return
     */
    @SuppressWarnings("static-access")
    public static PushPayload buildPushObject_all_tag_alertWithTitle(String tag,String tag_and,String notification_title, String msg_title, Map<String,String> extrasparam){
        String iosAlert = notification_title+":"+msg_title; 
        //需要最新sdk版本
//        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
        //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build()
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.tag(tag).tag_and(tag_and))
                //通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容
                                .setTitle(notification_title)//标题
                                .setAlert(msg_title)//内容
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、title、subtitle等
                                .incrBadge(1)//此项是指定此推送的badge自动加1
                                //.setSound("sound.caf")//设置声音
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                //.setContentAvailable(true)//是否可以在锁屏状态下接收
                                .build())    
                    .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    /**
     * 推送通知和自定义消息:根据tag推给个人
     * @param tag
     * @param tag_add
     * @param notification_title
     * @param msg_title
     * @param msg_content
     * @param extrasparam
     * @return
     */
    @SuppressWarnings("static-access")
    public static PushPayload buildPushObject_all_tag_alertAndmessage(String tag,String tag_and,String notification_title,String msg_title,String msg_content,Map<String, String> extrasparam ){
        String iosAlert = notification_title+":"+msg_title;
        //IosAlert.newBuilder().setTitleAndBody(notification_title, null, msg_title).setActionLocKey("PLAY").build()
        
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.tag(tag).tag_and(tag_and))
                //通知
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder()//指定不同平台的推送内容
                                .setTitle(notification_title)//标题
                                .setAlert(msg_title)//内容
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(iosAlert)//传一个IosAlert对象,指定apns title、body、subtitle等
                                .incrBadge(1)//此项是指定此推送的badge自动加1
                                //.setSound("sound.caf")//设置声音
                                //透传,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                //另一种方式: addExtra(key,value)
                                .addExtras(extrasparam)
                                //.setContentAvailable(true)//是否可以在锁屏状态下接收
                                .build())    
                    .build())
                //自定义消息
                .setMessage(Message.newBuilder()
                        .setTitle(msg_title)//消息标题
                        .setMsgContent(msg_content)//消息内容本身
                        //.setContentType("json")//消息内容类型
                        .addExtras(extrasparam)//json格式的可选参数
                        .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    /**
     * 推送自定义消息:根据tag推给个人
     * @param tag
     * @param msg_title
     * @param msg_content
     * @param extrasparam
     * @return
     */
    public static PushPayload buildPushObject_all_tags_message(String[] tag,String msg_title,String msg_content,Map<String, String> extrasparam ){
        
        return PushPayload.newBuilder()
                //推送平台
                .setPlatform(Platform.all())
                //推送目标:all、tag、tag_and、tag_not、alias、registration_id等
                .setAudience(Audience.tag(tag))
                //自定义消息
                .setMessage(Message.newBuilder()
                        .setTitle(msg_title)//消息标题
                        .setMsgContent(msg_content)//消息内容本身
                        //.setContentType("json")//消息内容类型
                        .addExtras(extrasparam)//json格式的可选参数
                        .build())
                //可选参数
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)//APNs是否为生产环境,false为开发环境
                        .setSendno(1)//推送编号
                        .setTimeToLive(86400)//指定本推送的离线保存时长(单位:秒),如果不传此字段则默认保存一天,最多指定保留十天
                        .build())
                .build();
    }
    
}
 声明:
 生产环境 : 打包后安装的应用 走的是生产环境。
 开发环境 : 连接真机测试时 应用走的是开发环境。
(只有ios需要配置环境,安卓不需要

可能有描述不清晰的地方,可以参考极光平台提供的API文档:https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#_7
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值