【Java工具】之極光推送(九)

為了以后方便,特把極光推送方法記錄下來。

  • 1、maven依賴
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.17</version>
</dependency>
  • 2、注冊極光,獲取密鑰
private static final String JI_GUANG_URL = "https://www.jiguang.cn/](https://www.jiguang.cn/";
private static final String JI_GUANG_API = "https://docs.jiguang.cn/jpush/server/sdk/java_sdk/";
protected static final Logger LOG = LoggerFactory.getLogger(JpushUtils.class);
private static final String appKey ="你的appKey";
private static final String masterSecret = "你的masterSecret";
private static final ClientConfig clientConfig = ClientConfig.getInstance();
private static final JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
  • 3.推送全部用戶
    ??推送平臺是全部,角標(biāo)數(shù)字為 5,通知聲音為默認(rèn) "default",并且附加字段 from = "JPush"、content和message。消息內(nèi)容是 message;ApnsProduction為true代表是生成環(huán)境,false是測試環(huán)境。
    /**
     * 推送全部用戶
     * @param notification
     * @param message
     */
    public static boolean pushAll(String notification, String message) {
        boolean b = false;
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(notification).setSound("default")
                                .setBadge(5)
                                .setContentAvailable(true).addExtra("from", "JPush").addExtra("content", message)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification).build())
                        .build())
                .setMessage(Message.content(message)).setOptions(Options.newBuilder().setApnsProduction(false).build())
                .build();
        try {
            PushResult result = jpushClient.sendPush(payload);
            b = true;
        } catch (APIConnectionException e) {
        } catch (APIRequestException e) {
        }
        return b;
    }
  • 4、根據(jù)別名進(jìn)行推送
/**
     * 根據(jù)別名進(jìn)行推送
     * @param notification
     * @param message  .setContentAvailable(true)
     */
    public static boolean pushSelected(String alias[], String notification, String message) {
        boolean b = false;
        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder().setAlert(notification).setSound("default")
                                .setBadge(5)
                                .addExtra("1", "http://tms.tikinmedia.com/upload/deviceVersion")
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(notification).build())
                        .build())
                //.setMessage(Message.content(message))
                .setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result);
            b = true;
        } catch (APIConnectionException e) {
        } catch (APIRequestException e) {
        }
        return b;
    }
  • 5、添加自定義附加字段
    /**
     * 安卓與ios平臺,通過別名推送
     * @param alias     別名
     * @param content   推送內(nèi)容
     * @param key       拓展字段key值
     * @param value     拓展字段value
     * @return
     */
    public static boolean buildPushObject_all_alias_alert(String[] alias, String content,String key,String value) {
        boolean b = false;
        PushPayload payload =  PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder().addExtra(key,value)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setBadge(5)
                                .setSound("default").addExtra(key,value)
                                .build())
                        .build())
                .build();
    try {
            PushResult pushResult = jpushClient.sendPush(payload);
            //解析數(shù)據(jù),0表示成功 
            //{"msg_id":3828060948763255,"sendno":180326298,"statusCode":0}
            System.out.println(pushResult);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
          return b;
    }

  • 6、結(jié)語

邁開腳步,再長的路也不在話下;停滯不前,再短的路也難以到達(dá)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 所選依賴: 工具類:一、 package com.xxx.core.util.push.enums; /** 配置...
    Y_LY閱讀 1,988評論 0 2
  • 本文主要介紹對接對接極光推送,包括發(fā)送自定義消息等。需要的朋友可以參考一下。 1.springboot對接極光推送...
    owen_7815閱讀 1,409評論 0 1
  • 應(yīng)用場景:在我們的項目中我們用的是創(chuàng)建視頻會議和預(yù)警消息通知推送這兩種情況,首先創(chuàng)建視頻會議和中途邀請人參加會議我...
    蛋皮皮652閱讀 3,852評論 1 2
  • package com.donghui.oa.util; import cn.jiguang.common.Cli...
    Leslie_Lee閱讀 682評論 0 0
  • 最近做了個極光推送的功能,一下是我對這個功能的一點理解; 首先要先介入極光的包,通過maven加載...
    FUJI0103閱讀 3,525評論 0 0

友情鏈接更多精彩內(nèi)容