1. 程式人生 > >java服務端的 極光推送

java服務端的 極光推送

dep obj exc sso asa 錯誤碼 返回 消息通道 port

項目中用到了極光推送 下面寫下筆記

首先引入jar包 下載地址https://docs.jiguang.cn/jpush/resources/(非maven項目的下載地址)

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

package com.test.push.messagepush01;

import java.util.Map; import cn.jiguang.common.ClientConfig; 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.SMS; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.audience.AudienceTarget; import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification;
public class PushTest{ private static final String MASTER_SECRET="9a3869c9d2fec8333db*****";//JPush服務器端下發的master_key private static final String APP_KEY="0e621306bf07eb4eefc*****";//JPush服務器端下發的app_key /** * 構建推送對象:對所有平臺,所有設備,內容為 alert的通知 * @param alter * @return */ public static PushPayload buildPushObject_all_all_alert(String alter) { return PushPayload.alertAll(alter); } /** * 所有平臺,推送目標是別名為 "alias",通知內容為 alert * @param alias * @param alert * @return */ public static PushPayload buildPushObject_all_alias_alert(String alias,Object alert) { return PushPayload.newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.alias(alias)) .setNotification(Notification.alert(alert)) .build(); } /** * 構建推送對象:平臺是 Android,目標是 tag的設備,通知內容是alert,並且標題為title。 * @param tag * @param alert * @param title * @param extras * @return */ public static PushPayload buildPushObject_android_tag_alertWithTitle(String tag,String alert,String title,Map<String, String> extras) { return PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.tag(tag)) .setNotification(Notification.android(alert, title, extras)) .build(); } /** * 構建推送對象:平臺是 iOS,推送目標是 tags(可以是一個設備也可以是多個設備),推送內容同時包括通知與消息 - 通知信息是alert,消息內容是 msgContent,角標數字為badge(應用程序左上角或者右上角的數字),通知聲音為sound,並且附加字段 from = "JPush"。 * 通知是 APNs 推送通道的,消息是 JPush 應用內消息通道的。 * APNs 的推送環境是“生產”(如果不顯式設置的話,Library 會默認指定為開發) * @param alert * @param msgContent * @param badge * @param sound * @param tags * @return */ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(Object alert,String msgContent,int badge,String sound,String...tags) { return PushPayload.newBuilder() .setPlatform(Platform.ios()) .setAudience(Audience.tag_and(tags)) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(alert) .setBadge(badge) .setSound(sound) .addExtra("from", "JPush") .build()) .build()) .setMessage(Message.content(msgContent)) .setOptions(Options.newBuilder() .setApnsProduction(true) .build()) .build(); } /** * 構建推送對象:平臺是 Andorid 與 iOS,推送的設備有(推送目標為tags和推送目標別名為aliases),推送內容是 - 內容為 msg_content的消息,並且附加字段 from = JPush。 * @param msg_content * @param tags * @param aliases * @return */ public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String msg_content,String[] tags,String[] aliases) { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.newBuilder() .addAudienceTarget(AudienceTarget.tag(tags)) .addAudienceTarget(AudienceTarget.alias(aliases)) .build()) .setMessage(Message.newBuilder() .setMsgContent(msg_content) .addExtra("from", "JPush") .build()) .build(); } /** * 構建推送對象:推送內容包含SMS信息 * @param title * @param sendSMSContent * @param delayTime * @param aliases */ public static void testSendWithSMS(String title,String sendSMSContent,int delayTime,String... aliases) { JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY); try { SMS sms = SMS.content(sendSMSContent, delayTime); PushResult result = jpushClient.sendAndroidMessageWithAlias(title, sendSMSContent, sms, aliases); System.out.println("Got result - " + result); } catch (APIConnectionException e) { System.out.println("Connection error. Should retry later. "+e); } catch (APIRequestException e) { System.out.println("Error response from JPush server. Should review and fix it. "+e); System.out.println("HTTP Status: " + e.getStatus()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Error Message: " + e.getErrorMessage()); } } public static void main(String[] args) { String master_secret=PushTest.MASTER_SECRET; String app_key=PushTest.APP_KEY; JPushClient jpushClient = new JPushClient(master_secret,app_key, null, ClientConfig.getInstance()); PushPayload payload = PushTest.buildPushObject_all_all_alert("消息推送"); //PushPayload payload=PhicommPush.buildPushObject_android_tag_alertWithTitle("tag1", "123", "123", null); try { PushResult result = jpushClient.sendPush(payload); System.out.println("Got result - " + result); } catch (APIConnectionException e) { // Connection error, should retry later System.out.print("Connection error, should retry later "+e); } catch (APIRequestException e) { // Should review the error, and fix the request System.out.println("根據返回的錯誤信息核查請求是否正確"+e); System.out.println("HTTP 狀態信息碼: " + e.getStatus()); System.out.println("JPush返回的錯誤碼: " + e.getErrorCode()); System.out.println("JPush返回的錯誤信息: " + e.getErrorMessage()); } } }

java服務端的 極光推送