Java极光推送

通过极光实现一个简单的推送功能。首先需要注册极光的账号,根据极光提供的文档先在开发者平台内创建一个应用。操作完之后你的安卓手机应该有一个JPush SDK Demo。进入应用中找到应用设置,找到你的AppKey和Master Secret:
在这里插入图片描述
新建一个java项目,引入jar包,jar包在极光官网上都有,新建一个类。
在这里插入图片描述

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
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.notification.Notification;

/**
 * 极光推送工具类
 * @author why
 *
 */
@SuppressWarnings("all")
public class JPushAllUtil {
    private final static String appKey = "将你自己的这个值写进来";
    private final static String masterSecret = "将你自己的这个值写进来";
    /**
     * 测试方法
     */
    public static void main(String[] args) {
        //发送通知
        jSend_notification("在安卓手机的app上获取的RegId","为什么总是Hello World!");
         
    }
    /**
     * 发送通知
     * @param registrationId 设备标识
     * @param alert 推送内容
     */
    public static void jSend_notification(String registrationId, String alert){
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
        PushPayload payload = send_N(registrationId, alert);
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result);
             
        } catch (APIConnectionException e) {
            System.out.println(e);
        } catch (APIRequestException e) {
            System.out.println(e);
            System.out.println("Error response from JPush server. Should review and fix it. "+ e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
            System.out.println("Msg ID: " + e.getMsgId());
        }
    }
     
    public static PushPayload send_N(String registrationId, String alert){
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())//必填    推送平台设置
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(Notification.alert(alert))
                /**
                 * 如果目标平台为 iOS 平台 需要在 options 
                 * 中通过 apns_production 字段来制定推送环境。
                 * True 表示推送生产环境,False 表示要推送开发环境; 如
                 * 果不指定则为推送生产环境
                 */
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .build())
                .build();
        }
}

如下图,点击下方按钮显示RegId:
在这里插入图片描述
运行java代码,控制台输出:
在这里插入图片描述
手机显示
在这里插入图片描述
例子不难,只是整理一下新知识。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值