jsp推送iOS消息

来源:互联网 发布:重庆知秋凤凰婚礼 编辑:程序博客网 时间:2024/03/29 04:32
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
    <%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="javapns.Push" %>
<%@ page import="javapns.devices.Device" %>
<%@ page import="javapns.devices.implementations.basic.BasicDevice" %>
<%@ page import="javapns.notification.AppleNotificationServerBasicImpl" %>
<%@ page import="javapns.notification.PushNotificationPayload" %>
<%@ page import="javapns.notification.PushNotificationManager" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="javapns.notification.PushedNotification" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String deviceToken = "AAAA5064209B6165C8542A9A17118BFFFFFE99851A9BD124B26CE6A6F84DDDDD";

    String alert = "this is  test push message";//push的内容
    int badge = 3;//图标小红圈的数值
    String sound = "default";//铃音


    List<String> tokens = new ArrayList<String>();
    tokens.add(deviceToken);
    String certificatePath = "D:\\etah\\P12\\iOS-development-cert.p12";
    String certificatePassword = "123456";//此处注意导出的证书密码不能为空因为空密码会报错
    boolean sendCount = true;


    try
    {
        PushNotificationPayload payLoad = new PushNotificationPayload();
        payLoad.addAlert(alert); // 消息内容
        payLoad.addBadge(badge); // iphone应用图标上小红圈上的数值
       
        if (!StringUtils.isBlank(sound))
        {
            payLoad.addSound(sound);//铃音
        }
        PushNotificationManager pushManager = new PushNotificationManager();
        //true:表示的是产品发布推送服务 false:表示的是产品测试推送服务
        pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificatePath, certificatePassword, false));
        List<PushedNotification> notifications = new ArrayList<PushedNotification>();
        // 发送push消息
        if (sendCount)
        {
            Device device = new BasicDevice();
            device.setToken(tokens.get(0));
            PushedNotification notification = pushManager.sendNotification(device, payLoad, true);
            notifications.add(notification);
        }
        else
        {
            List<Device> device = new ArrayList<Device>();
            for (String token : tokens)
            {
                device.add(new BasicDevice(token));
            }
            notifications = pushManager.sendNotifications(payLoad, device);
        }            
        pushManager.stopConnection();
}
catch (Exception ex)
{
ex.printStackTrace();
}


%>
</body>
</html>