使用阿里的开源包进行APNS推送

1、获取p12文件方法哦工程src目录下 找ios开发人员要
2、导入jar文件  <a target=_blank href="http://download.csdn.net/detail/shy323/9452329">http://download.csdn.net/detail/shy323/9452329</a>
3、copy我给的类 就可以测试apns推送了
</pre><pre name="code" class="java">package com.foxconn.util;

import cn.teaey.apns4j.keystore.KeyStoreHelper;
import cn.teaey.apns4j.keystore.KeyStoreWraper;
import cn.teaey.apns4j.network.AppleGateway;
import cn.teaey.apns4j.network.AppleNotificationServer;
import cn.teaey.apns4j.network.SecurityConnection;
import cn.teaey.apns4j.network.SecuritySocketFactory;
import cn.teaey.apns4j.protocol.NotifyPayload;

public class ApnsUtil {
	private static SecurityConnection securityConnection;
	// 获取apns推送链接
	private static SecurityConnection getSecurityConnection() {
		if (securityConnection == null) {
			try {
				// get a keystore 
				KeyStoreWraper keyStoreWrapper = KeyStoreHelper.getKeyStoreWraper("doctor.p12", "123456");
				// get apple server with env
				AppleNotificationServer appleNotificationServer = AppleNotificationServer.get(AppleGateway.ENV_DEVELOPMENT);
				// init ssl socket factory
				SecuritySocketFactory socketFactory = SecuritySocketFactory.Builder.newBuilder().appleServer(appleNotificationServer).keyStoreWrapper(keyStoreWrapper).build();
				// create a ssl socket
				securityConnection = SecurityConnection.newSecurityConnection(socketFactory);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println(securityConnection.hashCode());
		return securityConnection;
	}

	// 推送apns消息
	public static void sendNotification(String token, NotifyPayload notifyPayload) {
		try {
			getSecurityConnection().sendAndFlush(token, notifyPayload);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		NotifyPayload notifyPayload=NotifyPayload.newNotifyPayload();
		notifyPayload.sound("msg.mp3");
		notifyPayload.alertBody("How are you?");
		notifyPayload.badge(1);
		ApnsUtil.sendNotification("b356d706e73222705aed97012a7255a5a7356ae7c88914efa8d508a52f73f27b", notifyPayload);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠星曜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值