java sdk 极光推送 安卓,ios

项目需要jar包:
 
         
1
<dependency>
2
    <groupId>cn.jpush.api</groupId>
3
    <artifactId>jpush-client</artifactId>
4
    <version>3.2.17</version>
5
    </dependency>
6
    <dependency>
7
        <groupId>cn.jpush.api</groupId>
8
        <artifactId>jiguang-common</artifactId>
9
        <version>1.0.3</version>
10
    </dependency>
项目需要jar包所需依赖包:
 
          
1
   <dependency>
2
        <groupId>io.netty</groupId>
3
        <artifactId>netty-all</artifactId>
4
        <version>4.1.6.Final</version>
5
        <scope>compile</scope>
6
    </dependency>
7
    <dependency>
8
        <groupId>com.google.code.gson</groupId>
9
        <artifactId>gson</artifactId>
10
        <version>2.3</version>
11
    </dependency>
12
    <dependency>
13
            <groupId>com.alibaba</groupId>
14
            <artifactId>fastjson</artifactId>
15
            <version>1.2.5</version>

   </dependency>
17
    <dependency>
18
        <groupId>org.slf4j</groupId>
19
        <artifactId>slf4j-api</artifactId>
20
        <version>1.7.7</version>
21
    </dependency>
22
23
    <!-- For log4j -->
24
    <dependency>
25
        <groupId>org.slf4j</groupId>
26
        <artifactId>slf4j-log4j12</artifactId>
27
        <version>1.7.7</version>
28
    </dependency>
29
    <dependency>
30
        <groupId>log4j</groupId>
31
        <artifactId>log4j</artifactId>
32
        <version>1.2.17</version>
33
    </dependency>

审核通过后即可获得 appkey 和 appsecret:
APP_KEY_ALL= appkey  ,
MASTER_SECRET_ALL= appsecret

推送内容同时包括通知与消息
给所有平台发送通知:
 
         
1
        //签到推送
2
        public static void sendNotify(List<String> userIds) {
3
            JPushClient jpushClient = new cn.jpush.api.JPushClient(MASTER_SECRET_ALL, APP_KEY_ALL, null, ClientConfig.getInstance());
4
            PushPayload payload = PushPayload.newBuilder()
5
                    .setPlatform(Platform.all())   //
6
                    .setAudience(Audience.alias(userIds)).setOptions(Options.newBuilder().setApnsProduction(true).build())
7
                    .setNotification(Notification.newBuilder()
8
                  //setAlert设置在上面表示下面两个平台推送内容是一样的
9
10
                            .setAlert("定义通知的内容")  
11
                            .addPlatformNotification(AndroidNotification.newBuilder()  
12
                                    .build())
13
                            .addPlatformNotification(IosNotification.newBuilder()
14
                                    .incrBadge(1)
15
                                    .build())
16
                            .build())
17
                
18
                    .setOptions(Options.newBuilder()
19
                            .setApnsProduction(true)  //
20
                            .build())
21
                    .build();
22
            try {
23
                PushResult result = jpushClient.sendPush(payload);
24
                LOG.info("Got result - " + result);
25
                System.out.println("result,"+result);
26
27
            } catch (APIConnectionException e) {
28
                // Connection error, should retry later
29
                LOG.error("Connection error, should retry later", e);
30
31
            } catch (APIRequestException e) {
32
                // Should review the error, and fix the request
33
                LOG.error("Should review the error, and fix the request", e);
34
                LOG.info("HTTP Status: " + e.getStatus());
35
                LOG.info("Error Code: " + e.getErrorCode());
36
                LOG.info("Error Message: " + e.getErrorMessage());
37
                System.out.println("result,"+e.getErrorMessage());
38
            }
39
            System.out.println("Bye,mySendPush.");
40
        }
给所有平台发送消息:
1
public static void sendMessage(List<String> userIdList,JSONObject json) {
2
        
3
        JPushClient jpushClient = new cn.jpush.api.JPushClient(MASTER_SECRET_ALL, APP_KEY_ALL, null, ClientConfig.getInstance());
4
        PushPayload payload = PushPayload.newBuilder()
5
                .setPlatform(Platform.all())
6
                .setAudience(Audience.alias(userIdList)).setOptions(Options.newBuilder().setApnsProduction(true).build())
7
                .setMessage(Message.newBuilder()
8
                        .setMsgContent(JSONObject.toJSONString(json))  //消息内容,可以用json封装传送字段
9
                        //.addExtra("from", "JPush")   //也可以
10
                        .build())
11
                .setOptions(Options.newBuilder()
12
                        .setApnsProduction(true)
13
                        .build())
14
                .build();
15
        try {
16
            PushResult result = jpushClient.sendPush(payload);
17
            LOG.info("Got result - " + result);
18
            System.out.println("result,"+result);
19
20
        } catch (APIConnectionException e) {
21
            // Connection error, should retry later
22
            LOG.error("Connection error, should retry later", e);
23
24
        } catch (APIRequestException e) {
25
            // Should review the error, and fix the request
26
            LOG.error("Should review the error, and fix the request", e);
27
            LOG.info("HTTP Status: " + e.getStatus());
28
            LOG.info("Error Code: " + e.getErrorCode());
29
            LOG.info("Error Message: " + e.getErrorMessage());
30
            System.out.println("result,"+e.getErrorMessage());
31
        }
32
        System.out.println("Bye,mySendPush.");
33
    }
单独给ios推送通知和消息:
1
 public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
2
        return PushPayload.newBuilder()
3
                .setPlatform(Platform.ios())
4
                .setAudience(Audience.tag_and("tag1", "tag_all"))
5
                .setNotification(Notification.newBuilder()
6
                        .addPlatformNotification(IosNotification.newBuilder()
7
                                .setAlert(ALERT)
8
                                .setBadge(5)
9
                                .setSound("happy")
10
                                .addExtra("from", "JPush")
11
                                .build())
12
                        .build())
13
                 .setMessage(Message.content(MSG_CONTENT))
14
                 .setOptions(Options.newBuilder()
15
                         .setApnsProduction(true)
16
                         .build())
17
                 .build();
18
    }
解释: 平台是 iOS,推送目标 Audience 是 "tag1", "tag_all" 的交集,推送内容同时包括通知 setNotification 与消息 setMessage
 - 通知信息 setAlert 是 ALERT(自定义),角标数字 setBadge 为 5,通知声音 setSound 为 "happy",并且附加字段  addExtra为 from = "JPush";
-消息内容 setMessage 是 MSG_CONTENT (自定义)
-通知是 APNs 推送通道的,消息是 JPush 应用内消息通道的。APNs 的推送环境是“生产”(如果不显式设置的话,Library 会默认指定为开发)

其他平台形式推送消息和通知具体详看官方文档!



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值