前言:
目前小伙伴们对“java向app推送消息”都比较关心,你们都需要学习一些“java向app推送消息”的相关资讯。那么小编同时在网摘上汇集了一些关于“java向app推送消息””的相关文章,希望咱们能喜欢,看官们快快来学习一下吧!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.audience.AudienceTarget;import cn.jpush.api.push.model.notification.AndroidNotification;import cn.jpush.api.push.model.notification.IosNotification;import cn.jpush.api.push.model.notification.Notification;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.HashMap;import java.util.Map;import static org.apache.http.client.methods.RequestBuilder.put;//推送工具类public class Jdpush { protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class); // app_key 和 master_secret 这两个是在推送官网申请的 private static String APP_KEY = "xxxxxxxxxxxxxxxxxxxxx"; private static String MASTER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; //测试用 public static final String TITLE = "都看过来"; public static final String ALERT = "祝你幸福"; public static final String MSG_CONTENT = "生生世世所"; public static final String REGISTRATION_ID = "1114a89xxxxbe5c3eb"; public static final String TAG = "tag_api"; public static JPushClient jpushClient=null; /** * 全部推送 */ public static void testSendPush() { jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, 3); //生成推送的内容,这里我们先测试全部推送 PushPayload payload=buildPushObject_all_alias_alerts(); try { System.out.println(payload.toString()); PushResult result = jpushClient.sendPush(payload); System.out.println(result+"................................"); 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()); } } /** * 指定推送(携带参数) * pushId--推送的识别号 ALERT--推送内容 【推送用】下面的参数 用户点击跳转用 * type--[1-出售 2-购买 3-xx 4-xx] productId-- 订单id * isType-- 1-我的交易记录详情页 2-我的发布详情页 3-列表详情页 4-区域经理详情页 */ public static void userSendPush(String pushId,String ALERT,Integer type,Long productId,Integer isType) { jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, 3); //生成推送的内容,这里我们先测试全部推送 PushPayload payload=buildPushObject_all_alias_alert(pushId,ALERT,type,productId,isType); try { System.out.println(payload.toString()); PushResult result = jpushClient.sendPush(payload); System.out.println(result+"................................"); 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()); } } /** * 指定推送(无参数 只有内容) */ public static void userSendPushs(String pushId,String ALERT) { jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, 3); //生成推送的内容,这里我们先测试全部推送 PushPayload payload=buildPushObject_alias_alert(pushId,ALERT); try { System.out.println(payload.toString()); PushResult result = jpushClient.sendPush(payload); System.out.println(result+"................................"); 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()); } } //指定推送 public static PushPayload buildPushObject_all_alias_alert(String pushId,String alert,Integer type,Long productId,Integer isType) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.registrationId(pushId))//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到// .setNotification(Notification.alert(alert)) .setNotification(Notification.newBuilder() .addPlatformNotification(IosNotification.newBuilder() .setAlert(alert) .addExtra("isType",isType) .addExtra("type",type) .addExtra("productId",productId) .build()) .build())// .setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("type",type).addExtra("isType",isType).addExtra("prId",prId).build()) .build(); } //发送全部 public static void main(String[] args) {// Jdpush.userSendPush("171976fa8a18b38cdbf","今天是星期三"); } //广播推送 public static PushPayload buildPushObject_all_alias_alerts() { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到 .setNotification(Notification.alert(ALERT)) .build(); } public static PushPayload buildPushObject_all_all_alert() { return PushPayload.alertAll(ALERT); } public static PushPayload buildPushObject_android_tag_alertWithTitle() { return PushPayload.newBuilder() .setPlatform(Platform.android()) .setAudience(Audience.all()) .setNotification(Notification.android(ALERT, TITLE, null)) .build(); } public static PushPayload buildPushObject_android_and_ios() { return PushPayload.newBuilder() .setPlatform(Platform.android_ios()) .setAudience(Audience.tag("tag1")) .setNotification(Notification.newBuilder() .setAlert("alert content") .addPlatformNotification(AndroidNotification.newBuilder() .setTitle("Android Title").build()) .addPlatformNotification(IosNotification.newBuilder() .incrBadge(1) .addExtra("extra_key", "extra_value").build()) .build()) .build(); } 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(); } 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(); } //指定推送 (无参数) public static PushPayload buildPushObject_alias_alert(String pushId,String ALERT) { return PushPayload.newBuilder() .setPlatform(Platform.all())//设置接受的平台 .setAudience(Audience.registrationId(pushId))//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到 .setNotification(Notification.alert(ALERT)) .build(); }}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java向app推送消息