Javapns-sdk16-2.2.1多线程推送

来源:互联网 发布:中科大软件学院学费 编辑:程序博客网 时间:2024/05/01 19:45
package com.hero.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hero.model.ResponseModel;

import java.util.ArrayList;

import javapns.Push;
import javapns.communication.exceptions.*;
import javapns.devices.*;
import javapns.devices.exceptions.*;
import javapns.devices.implementations.basic.*;
import javapns.feedback.*;
import javapns.notification.*;
import javapns.notification.transmission.*;
import javapns.notification.PushNotificationPayload;

@Controller
public class PushController {

    @RequestMapping(value="push",produces = {"application/json;charset=UTF-8"},method=RequestMethod.GET)
    public @ResponseBody ResponseModel pushMethod () {
        
        String keystore = "C:\\push.p12";//证书路径和证书名  
        String password = "zhouleizhao"; // 证书密码  
        String token = "1ba52f6e6b48a54d7ec192a0c91ab7d8c4b9f902e257e7a9799b61f555b949";// 手机唯一标识,不允许有空格 
        boolean production = false; // 设置true为正式服务地址,false为开发者地址  
        int threadThreads = 10;// 线程数  
//        try{
//            Push.alert("zhouleizhao", keystore, password, production, token);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        try {  
            // 建立与Apple服务器连接  
            AppleNotificationServer server = new AppleNotificationServerBasicImpl(keystore, password, production );  
            List<PayloadPerDevice> list = new ArrayList<PayloadPerDevice>();  
            PushNotificationPayload payload = new PushNotificationPayload();
            payload.addAlert("推送内容");  
            payload.addSound("default");// 声音  
            payload.addBadge(1);//图标小红圈的数值  
            payload.addCustomDictionary("url","www.baidu.com");// 添加字典   
            PayloadPerDevice pay = new PayloadPerDevice(payload,token);// 将要推送的消息和手机唯一标识绑定  
            list.add(pay);  
      
            NotificationThreads work = new NotificationThreads(server,list,threadThreads);//   
            work.setListener(DEBUGGING_PROGRESS_LISTENER);// 对线程的监听,一定要加上这个监听  
            work.start(); // 启动线程  
            work.waitForAllThreads();// 等待所有线程启动完成  
       } catch (Exception e) {  
        e.printStackTrace();  
     }
        
        return null;
    }
    
    // 线程监听  
    public static final NotificationProgressListener DEBUGGING_PROGRESS_LISTENER = new NotificationProgressListener() {  
            public void eventThreadStarted(NotificationThread notificationThread) {  
                System.out.println("   [EVENT]: thread #" + notificationThread.getThreadNumber() + " started with " + " devices beginning at message id #" + notificationThread.getFirstMessageIdentifier());  
            }  
            public void eventThreadFinished(NotificationThread thread) {  
                System.out.println("   [EVENT]: thread #" + thread.getThreadNumber() + " finished: pushed messages #" + thread.getFirstMessageIdentifier() + " to " + thread.getLastMessageIdentifier() + " toward "+ " devices");  
            }  
            public void eventConnectionRestarted(NotificationThread thread) {  
                System.out.println("   [EVENT]: connection restarted in thread #" + thread.getThreadNumber() + " because it reached " + thread.getMaxNotificationsPerConnection() + " notifications per connection");  
            }  
            public void eventAllThreadsStarted(NotificationThreads notificationThreads) {  
                System.out.println("   [EVENT]: all threads started: " + notificationThreads.getThreads().size());  
            }  
            public void eventAllThreadsFinished(NotificationThreads notificationThreads) {  
                System.out.println("   [EVENT]: all threads finished: " + notificationThreads.getThreads().size());  
            }  
            public void eventCriticalException(NotificationThread notificationThread, Exception exception) {  
                System.out.println("   [EVENT]: critical exception occurred: " + exception);  
            }  
         };  
}



0 0