极光推送dome

第一次做极光推送,一个小的总结

极光推送是:使得开发者可以即时地向其应用程序的用户推送通知或者消息,简单的说就是通过JPush后台管理网站进行app消息的推送。可以让用户及时的收到最新的消息提示。

maven集成

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

工具类:

package com.nav.util;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.api.push.model.notification.AndroidNotification;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushClient;
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.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
@SuppressWarnings("all")
public class PushUtil {

    protected static final Logger LOG = LoggerFactory.getLogger(PushUtil.class);
    private static final String appKey = "*******************";
    private static final String masterSecret = "********************";

    //推送通知栏给所有用户
    public static void pushAll(){
        //定义一个alert的信息
        String content = "nvkj";
        //初始化创建连接
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        //把信息组装成一个发送的对象
        PushPayload payload = buildPushObject_all_all_alert(content);
        try { 
            //sendPush发送组装好的信息,返回结果PushResult 
            PushResult result = jpushClient.sendPush(payload);  
        catch (APIConnectionException e) { 
            LOG.error("Connection error. Should retry later. ", e); 
        } catch (APIRequestException e) { 
            LOG.error( "Error response from JPush server. Should review and fix it. ", e); 
    }

    //组装通知栏pushAll方法发送的信息
    public static PushPayload buildPushObject_all_all_alert(String content){
        return new PushPayload.alertAll(content);
    }

    
    /** * 推送通知栏消息并携带参数   安卓和ios通用
        * @param content 通知栏消息 
        * @param map 参数 */ 
    public static void alertAllMessageWithExtraData(String content,Map<String,String> map) { 
        JPushClient jpushClient = new JPushClient(masterSecret, appKey); 
        // For push, all you need do is to build PushPayload object. 
        PushPayload payload = buildPushObject_android_ios_alert(content,map); 
        try { 
            PushResult result = jpushClient.sendPush(payload); 
            LOG.info("Got result - " + result); } 
        catch (APIConnectionException e) { 
            LOG.error("Connection error. Should retry later. ", e); 
        } catch (APIRequestException e) { 
            LOG.error( "Error response from JPush server. Should review and fix it. ", e); 
            LOG.info("HTTP Status: " + e.getStatus()); LOG.info("Error Code: " + e.getErrorCode()); 
            LOG.info("Error Message: " + e.getErrorMessage()); LOG.info("Msg ID: " + e.getMsgId()); 
        } 
      }

    /** * @param 组装推送通知栏消息并携带参数 安卓和ios通用 
        * @param 参数 * @return 
     */ 
    public static PushPayload buildPushObject_android_ios_alert(String alert,Map<String,String> map){ 
            return PushPayload.newBuilder() 
                    .setPlatform(Platform.android_ios())    
                    .setAudience(Audience.all()) 
                    .setPlatform(Platform.all()) 
                    .setNotification(Notification.newBuilder() 
                    .addPlatformNotification(AndroidNotification.newBuilder() 
                    .addExtras(map) 
                    .setAlert(alert) 
                    .build()) 
                    .addPlatformNotification(IosNotification.newBuilder()     
                    .addExtras(map) .setAlert(alert) 
                     .build()) 
                     .build()) 
                     .build(); 
       }
   
    /**
     * @Title: PushByTag 
     * @Description: 构建推送对象点对点推送  平台安卓 ios   目标tag
     * @return
     */
    public static void PushByTag(String alert, String msg, String deviceType,
            String type, String tag) {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        Map<String, String> extras = new HashMap<String, String>();
        extras.put("id", msg);
        extras.put("type", type);
        PushPayload androidPayload = buildPushObject_android_tag_alertWithTitle(alert,
                tag, extras);
        PushPayload iosPayload = buildPushObject_ios_tag_alertWithTitle(alert,
                tag, extras);
        try {
            PushResult andoridResult = jpushClient.sendPush(androidPayload);
            LOG.info("Got result - " + andoridResult);
            PushResult iosResult = jpushClient.sendPush(iosPayload);
            LOG.info("Got result - " + iosResult);

        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
            LOG.error(
                    "Error response from JPush server. Should review and fix it. ",
                    e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
        }
    }

    /** * @Title: PushByTag 
    * @Description: 构建推送对象点对点推送 平台安卓 目标tag 
    * @return */
    public static PushPayload buildPushObject_android_tag_alertWithTitle(
            String title, String tag, Map<String, String> extras) {
        return PushPayload.newBuilder().setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(title, title, extras))
                .build();

    }
    /** * @Title: PushByTag
    * @Description: 构建推送对象点对点推送 iOS 目标tag
    * @return */
    public static PushPayload buildPushObject_ios_tag_alertWithTitle(
            String title, String tag, Map<String, String> extras) {
        return PushPayload.newBuilder().setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.ios(title, extras)).build();

    }


   }

构建推送对象

  • 快捷地构建推送对象:所有平台,所有设备,内容为 ALERT 的通知。
  •  public static PushPayload buildPushObject_all_all_alert() {
            return PushPayload.alertAll(ALERT);
        }
  • 构建推送对象:所有平台,推送目标是别名为 "alias1",通知内容为 ALERT。
    public static PushPayload buildPushObject_all_alias_alert() {
            return PushPayload.newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.alias("alias1"))
                    .setNotification(Notification.alert(ALERT))
                    .build();
        }
  • 构建推送对象:平台是 iOS,推送目标是 "tag1", "tag_all" 的交集,推送内容同时包括通知与消息 - 通知信息是 ALERT,角标数字为 5,通知声音为 "happy",并且附加字段 from = "JPush";消息内容是 MSG_CONTENT。通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
            return PushPayload.newBuilder()
                    .setPlatform(Platform.ios())
                    .setAudience(Audience.tag_and("tag1", "tag_all"))
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .setAlert(ALERT)
                                    .setBadge(5)
                                    .setSound("happy")
                                    .addExtra("from", "JPush")
                                    .build())
                            .build())
                     .setMessage(Message.content(MSG_CONTENT))
                     .setOptions(Options.newBuilder()
                             .setApnsProduction(true)
                             .build())
                     .build();
        }
  • 构建推送对象:平台是 Andorid 与 iOS,推送目标是 ("tag1" 与 "tag2" 的并集)交("alias1" 与 "alias2" 的并集),推送内容是 - 内容为 MSG_CONTENT 的消息,并且附加字段 from = JPush。
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
            return PushPayload.newBuilder()
                    .setPlatform(Platform.android_ios())
                    .setAudience(Audience.newBuilder()
                            .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                            .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
                            .build())
                    .setMessage(Message.newBuilder()
                            .setMsgContent(MSG_CONTENT)
                            .addExtra("from", "JPush")
                            .build())
                    .build();
        }
  • 构建推送对象:推送内容包含SMS信息
    public static void testSendWithSMS() {
            JPushClient jpushClient = new JPushClient(masterSecret, appKey);
            try {
                SMS sms = SMS.content("Test SMS", 10);
                PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
                LOG.info("Got result - " + result);
            } catch (APIConnectionException e) {
                LOG.error("Connection error. Should retry later. ", e);
            } catch (APIRequestException e) {
                LOG.error("Error response from JPush server. Should review and fix it. ", e);
                LOG.info("HTTP Status: " + e.getStatus());
                LOG.info("Error Code: " + e.getErrorCode());
                LOG.info("Error Message: " + e.getErrorMessage());
            }
        }
    

    统计获取样例

  • 构建推送对象:平台是 Android,目标是 tag 为 "tag1" 的设备,内容是 Android 通知 ALERT,并且标题为 TITLE。
    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
            return PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.tag("tag1"))
                    .setNotification(Notification.android(ALERT, TITLE, null))
                    .build();
        }

转载于:https://my.oschina.net/niuruijing/blog/859199

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值