`
shendixiong
  • 浏览: 393083 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

apple 苹果推送

阅读更多

推送注意点:

     1.需要证书以及密码

     2.deviceid正确,手机安装了你申请证书的应用以及该deviceid设备的推送服务已经开启了

     3.苹果官方文档说明,推送的内容不能大于256个字节,否则会报错!所以大于该字节,建议用“...”

     4.需要相关的jar 以及在博客的附件中了

 

 

package test.common;

import java.util.ArrayList;
import java.util.List;

import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.AppleNotificationServerBasicImpl;
import javapns.notification.PushNotificationManager;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;

public class LocalTest {
	public static void main(String[] args) {
		pushApple();
	}
	
	public static void pushApple() {
		String deviceToken = "此处填写你要推送的deviceid";
		// push的内容
		String alert = "推送消息内容";
		// 图标小红圈的数值
		int badge = 1;
		
		List<String> tokens = new ArrayList<String>();
		// deviceid 集合
		tokens.add(deviceToken);
		// 证书地址
		String certificatePath = "E:/iphone/xchatpush_dev.p12";
		// 此处注意导出的证书密码不能为空因为空密码会报错
		String certificatePassword = "abc123";
		// 批量推送标识
		boolean sendCount = true;
		try {
			PushNotificationPayload payLoad = new PushNotificationPayload();
			// 推送消息内容
			payLoad.addAlert(alert); 
			// iphone应用图标上小红圈上的数值
			payLoad.addBadge(badge); 
			//设置铃音 此处为默认铃声
            payLoad.addSound("default");
			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);
			}
			List<PushedNotification> failedNotifications = PushedNotification
					.findFailedNotifications(notifications);
			List<PushedNotification> successfulNotifications = PushedNotification
					.findSuccessfulNotifications(notifications);
			// 推送失败的条数
			int failed = failedNotifications.size();
			// 推送成功的条数
			int successful = successfulNotifications.size();
			// 关闭conn
			pushManager.stopConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics