美文网首页Java知识书屋
【Java工具】之极光推送(九)

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

作者: 3d0829501918 | 来源:发表于2019-06-13 18:02 被阅读15次

为了以后方便,特把极光推送方法记录下来。

  • 1、maven依赖
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.17</version>
</dependency>
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.推送全部用户
      推送平台是全部,角标数字为 5,通知声音为默认 "default",并且附加字段 from = "JPush"、content和message。消息内容是 message;ApnsProduction为true代表是生成环境,false是测试环境。
    /**
     * 推送全部用户
     * @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、根据别名进行推送
/**
     * 根据别名进行推送
     * @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   推送内容
     * @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);
            //解析数据,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、结语

迈开脚步,再长的路也不在话下;停滞不前,再短的路也难以到达。

相关文章

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

    为了以后方便,特把极光推送方法记录下来。 1、maven依赖 2、注册极光,获取密钥  极光地址:https://...

  • 极光推送 java

    mave依赖cn.jpush.api jpush-c...

  • Java极光推送

    极光官方java sdk文档http://www.jianshu.com/p/e1f6335d40f0 1.注册 ...

  • Java SDK 实现极光推送

    公司项目,要做一个告警推送,将告警信息推送到手机上。我采取的是现比较流行的极光推送。 附上极光推送 Java SD...

  • 极光推送工具类

    所选依赖: 工具类:一、 package com.xxx.core.util.push.enums; /** 配置...

  • 极光推送

    极光推送视频地址,非常详细的极光推送视频 极光推送

  • 极光推送,java后台。

    官方demo下载 由于项目需要推送功能,通过讨论,初步使用第三方平台推送,因为自己弄的话,时间上不允许,所以,就...

  • 极光推送 java集成

    **移动端设备: **Android:走自建的TCP长连接通道iOS : 走自家的系统推送通道,WinPhone:...

  • 极光推送 java util

    publicclassJPushUtil { /** * 安卓iOS标签推送 *@paramtagValues 标...

  • 极光推送

    极光推送 tagprivate void initJpush() {//TODO 极光推送// JPushInte...

网友评论

    本文标题:【Java工具】之极光推送(九)

    本文链接:https://www.haomeiwen.com/subject/fpxefctx.html