java使用极光推送根据tag进行定期定时发送自定义标题和内容

应公司需求开发,特此记录:

java使用极光推送根据tag进行定期定时发送自定义标题和内容

极光定时推送可参考官方代码:

https://github.com/jpush/jpush-api-java-client/blob/master/example/main/java/cn/jpush/api/examples/ScheduleExample.java

下面是自己写的测试代码

    @Test
    public void testpush() {
        Set tagSet = new HashSet<>();
        tagSet.add("ChineseSimplified");
        tagSet.add("English");
        CreateDailySchedule("2019-07-06 12:16:13", "2019-08-06 12:16:13", "11:10:00", tagSet, 86400, 0, "你好", "yongxuezhen");
    }


/**
     * 极光根据tag定期进行推送
     *
     * @param starttime    开始日期   eg:"2019-07-06 12:16:13"
     * @param endtime      结束日期   eg:"2019-08-06 12:16:13"
     * @param time         发送时间   eg:  "19:46:00"
     * @param tagSet       要给tagSet内的多个tag进行发送通知 存放tag
     * @param time_to_live 设置离线消息保留时长(秒),默认 86400 (1 天),最长 10 天。该字段对 iOS 的 Notification 消息无效。
     * @param plat         推送的环境【0:Android 1:IOS生产平台 2:IOS开发平台 3:Android和IOS生产平台 4:Android和IOS开发平台 5:什么都没选】
     * @param title        要推送的标题
     * @param content      要推送的内容
     */
    public void CreateDailySchedule(String starttime, String endtime, String time, Set tagSet, int time_to_live, int plat, String title, String content) {
        //在极光注册应用即可得到下述,两参数
        String masterSecret = ""; 
        String appKey = "";
        JPushClient jPushClient = new JPushClient(masterSecret, appKey);
        //设置定期发送的标题
        String name = "定期发送";
        /*
         *   想要进行极光的定期自定义发送通知,需要创建PushPayload作为push参数
         *   PushPayload具体需要设置四个对象进行封装才可最终使用:Options,Platform,Audience,Notification
         *   以下是根据自定义的消息进行一一设置
         */
        //设置Options , platform,audience
        Options options = Options.newBuilder().build();
        Platform platform = null;
        Audience audience = Audience.tag(tagSet);
        Notification notification = null;
        options.setTimeToLive(time_to_live);
        HashMap map = new HashMap<>();
        map.put("type", "infomation");
        Notification.Builder builder = Notification.newBuilder();
        IosAlert alert = IosAlert.newBuilder()
				.setTitleAndBody(title,"", content)
				.build();
builder.addPlatformNotification(AndroidNotification.newBuilder().setAlert(content).setTitle(title).addExtras(map).build());
        builder.addPlatformNotification(IosNotification.newBuilder().setAlert(alert).addExtras(map).build());
        switch (plat) {
            case 0:
                platform = Platform.android();
                notification = Notification.android(content, title, map);
                break;
            case 1:
                notification = Notification.ios(alert, map);
                platform = Platform.ios();
                options.setApnsProduction(true);
                break;
            case 2:
                notification = Notification.ios(alert, map);
                platform = Platform.ios();
                options.setApnsProduction(false);
                break;
            case 3:
                notification = builder.build();
                platform = Platform.android_ios();
                options.setApnsProduction(true);
                break;
            case 4:
                notification = builder.build();
                platform = Platform.android_ios();
                options.setApnsProduction(false);
                break;
            case 5:
                break;
        }
        PushPayload build = PushPayload.newBuilder().setPlatform(platform).setOptions(options).setAudience(audience).setNotification(notification).build();
        try {
            ScheduleResult result = jPushClient.createDailySchedule(name, starttime, endtime, time, build);
            System.out.println("schedule result is " + result);
        } catch (APIConnectionException e) {
            System.out.println("Connection error. Should retry later. ");
        } catch (APIRequestException e) {
            System.out.println("Error response from JPush server. Should review and fix it. ");
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
        }
    }

有需要的如果有问题可以在下方评论,一起交流学习

你可能感兴趣的:(学习)