关闭

极光推送案例-PushExample-Jpush

标签: jpushJPushClient极光推送
3398人阅读 评论(1) 收藏 举报
分类:

ssh - maven - java项目-极光注册id完成推送


这是我学习时的步骤:


1:去极光推送平台注册账号,自己可以去注册(一般公司会帮助完成注册)

地址:https://www.jpush.cn


2:进入控制台

点击创建应用(不细说,都有说明)

创建应用后会产生:(AppKey : dd1066407b044738b6479275 Master Secret : 2b38ce69b1de2a7fa95706ea)


3:客户端安装新的应用,会有一个极光注册id,找他们要就可以了,他们知道是什么东西的(不再多说)

Registration ID :02070ec9ab3


4:pom.xml

<dependency>
      <groupId>cn.jpush.api</groupId>
      <artifactId>jpush-client</artifactId>
      <version>3.1.3</version>
</dependency>


5:PushExample

package com.zpms.portal.management.user.action;

import java.util.Collection;
import java.util.LinkedList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
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;


public class PushExample {
    protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

   private static final String appKey ="dd1066407b044738b6479275";
   private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";

    public static final String TITLE = "Test from API example";
    public static final String ALERT = "Test from API Example - alert";
    public static final String MSG_CONTENT = "Test from API Example - msgContent";
    public static final String REGISTRATION_ID = "02070ec9ab3";
    public static final String TAG = "tag_api";

    public static void main(String[] args) {
        testSendPush();
    }
    
    
    public static void testSendPush() {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
        
        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_all_registrationid_alert();

        /*

         * Collection<String> strings = new LinkedList<String>();

         * strings.add(REGISTRATION_ID);//配置多个客户端的极光注册id

         * PushPayload payload = buildPushObject_all_registrationids_alert(strings);

         */       

        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);
            
        } catch (APIConnectionException e) {
            System.out.println("Connection error. Should retry later. " + e);
            
        } catch (APIRequestException 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 buildPushObject_all_registrationid_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all()) //设置平台-所有平台
                .setAudience(Audience.registrationId(REGISTRATION_ID)) //设置受众-极光注册id
                .setNotification(Notification.alert(ALERT)) //设置通知 - 消息
                .build();
    }
   

    //多个客户端
    public static PushPayload buildPushObject_all_registrationids_alert(Collection<String> strings) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all()) //设置平台-所有平台
                .setAudience(Audience.registrationId(strings)) //设置受众-极光注册id-多个客户端
                .setNotification(Notification.alert(ALERT)) //设置通知-推送信息
                .build();
    }
}


------------------------------------------------------------------------------------------------------------------------------------------

参考过的网页:http://www.cnblogs.com/jiqing9006/p/3945095.html

访问官方网站:http://docs.jpush.io

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58870次
    • 积分:686
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:10篇
    • 译文:0篇
    • 评论:10条
    最新评论
    文章分类