极光推送案例-PushExample-Jpush

ssh - maven - java项目-极光注冊id完毕推送


这是我学习时的步骤:


1:去极光推送平台注冊账号,自己能够去注冊(一般公司会帮助完毕注冊)

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


2:进入控制台

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

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


3:client安装新的应用。会有一个极光注冊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);//配置多个client的极光注冊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());
        }
    }
    
    //发给一个client
    public static PushPayload buildPushObject_all_registrationid_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all()) //设置平台-全部平台
                .setAudience(Audience.registrationId(REGISTRATION_ID)) //设置受众-极光注冊id
                .setNotification(Notification.alert(ALERT)) //设置通知 - 消息
                .build();
    }
   

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


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

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

訪问官方站点:http://docs.jpush.io

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值