springboot整合极光推送实现APP通知

JPush 简介

JPush 是经过考验的大规模 App 推送平台,每天推送消息数超过 5 亿条。 开发者集成 SDK 后,可以通过调用 API 推送消息。同时,JPush 提供可视化的 web 端控制台发送通知,统计分析推送效果。 JPush 全面支持 Android, iOS, Winphone 三大手机平台。

消息形式

JPush 提供四种消息形式:通知,自定义消息,富媒体和本地通知。

推送目标

通过使用标签,别名,Registration ID 和用户分群,开发者可以向特定的一个或多个用户推送消息。

快速开始

  • 到极光推送官方网站注册开发者帐号
  • 登录进入管理控制台,创建应用程序,得到 Appkey(SDK 与服务器端通过 Appkey 互相识别);
  • 在推送设置中给 Android 设置包名、给 iOS 上传证书、启用 WinPhone,根据你的需求进行选择;
  • 下载 SDK 集成到 App 里。

1.依赖

        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jpush-client</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>cn.jpush.api</groupId>
            <artifactId>jiguang-common</artifactId>
            <version>1.0.8</version>
        </dependency>

2.代码

package com.huasharp.smartapartment.util;

import cn.jiguang.common.ClientConfig;
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 com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.huasharp.smartapartment.entity.Usermssage;

import java.util.*;

/**
 *
 */
public class JpushManager {

    private static JpushManager instance;

    private String appkey = null;
    private String appMasterSecret = null;
    private JPushClient jpushClient = null;

    /**
     * @return
     */
    public static JpushManager getInstance() {
        if (instance == null) {
            instance = new JpushManager("56565165", "65956599681");
        }
        return instance;
    }

    /**
     * @param key
     * @param secret
     */
    private JpushManager(String key, String secret) {
        try {
            appkey = key;
            appMasterSecret = secret;

            jpushClient = new JPushClient(appMasterSecret, appkey, null, ClientConfig.getInstance());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param alias   别名
     * @param
     * @param title   标题
     * @param content 内容
     * @param
     * @param
     * @param isRing
     * @throws Exception
     */
    public void send(Collection<String> alias, String title, String content, String type, boolean isRing) throws Exception {
        try {
            //推送安卓
            AndroidNotification androidNotification = AndroidNotification.newBuilder().setAlert(content).addExtra("type", type).build();

            PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android())
                    .setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.alias(alias)).build())
                    .setNotification(Notification.newBuilder().addPlatformNotification(androidNotification).build())
                    .setMessage(Message.newBuilder().setTitle(title).setMsgContent(content).build()).build();
            PushResult result = jpushClient.sendPush(payload);
            System.err.println(result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            //ios
            IosNotification iosNotification = IosNotification.newBuilder().setAlert(content).addExtra("type", type).setBadge(5)
                    .setSound("Alarm.aif").build();
            PushPayload payloadIos = PushPayload.newBuilder().setPlatform(Platform.ios())
                    .setAudience(Audience.newBuilder().addAudienceTarget(AudienceTarget.alias(alias)).build())
                    .setOptions(Options.newBuilder().setApnsProduction(true).build())//true-推送生产环境 false-推送开发环境(测试使用参数)
                    .setNotification(Notification.newBuilder().addPlatformNotification(iosNotification).build())
                    .build();
            jpushClient.sendPush(payloadIos);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {// 极光推送
            Usermssage usermssage = new Usermssage();
            usermssage.setUserid("1");
            usermssage.setCategory("2");
            usermssage.setMessagetype(4);
            usermssage.setResourceurl("http://47.120.13.865:8001/huasharp/file/用户须知.docx");
            usermssage.setContent("欢迎您成为平台用户,我们将热忱为您服务。请点击阅读《用户须知》");
            usermssage.setStatus(0);
            usermssage.setCommonid(null);
            usermssage.setSendtime(new Date());
            usermssage.setCreatetime(new Date());
            usermssage.setModifytime(new Date());
            usermssage.setTitle("注册信息");
            List<String> userList = new ArrayList<>();
            userList.add("441900U00000044");
            String type = JSONObject.toJSONString(usermssage, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero);
            try {
                    JpushManager.getInstance().send(userList, "提示", "欢迎您成为平台用户,我们将热忱为您服务。请点击阅读《用户须知》", type, true);

            } catch (Exception e) {
                e.printStackTrace();
            }
               
    }
}

 

posted @ 2020-11-02 16:52  william_zhao  阅读(1526)  评论(0编辑  收藏  举报