JAVA APNS苹果推送

最近在做苹果推送,找到了一个比较好的开源项目,国人写的

下载地址为:https://github.com/RamosLi/dbay-apns-for-java

调用代码如下:

import java.io.InputStream;
import java.util.List;

import com.dbay.apns4j.IApnsService;
import com.dbay.apns4j.impl.ApnsServiceImpl;
import com.dbay.apns4j.model.ApnsConfig;
import com.dbay.apns4j.model.Feedback;
import com.dbay.apns4j.model.Payload;

/**
 * @author RamosLi
 *
 */
public class Apns4jDemo {
	private static IApnsService apnsService;
	
	private static IApnsService getApnsService() {
		if (apnsService == null) {
			ApnsConfig config = new ApnsConfig();
			InputStream is = Apns4jDemo.class.getClassLoader().getResourceAsStream("Certificate.p12");
			config.setKeyStore(is);
			config.setDevEnv(false);
			config.setPassword("123123");
			config.setPoolSize(5);
			apnsService = ApnsServiceImpl.createInstance(config);
		}
		return apnsService;
	}
	
	public static void main(String[] args) {
		IApnsService service = getApnsService();
		
		// send notification
		String token = "94c4764e4645f42a7b2052692c8b5b41f9d5c925876e11fec5721e9045ee4e5b";
		
		Payload payload = new Payload();
		payload.setAlert("How are you?");
		// If this property is absent, the badge is not changed. To remove the badge, set the value of this property to 0
		payload.setBadge(1);
		// set sound null, the music won't be played
//		payload.setSound(null);
		payload.setSound("msg.mp3");
		payload.addParam("uid", 123456);
		payload.addParam("type", 12);
		service.sendNotification(token, payload);
		
		// payload, use loc string
		Payload payload2 = new Payload();
		payload2.setBadge(1);
		payload2.setAlertLocKey("GAME_PLAY_REQUEST_FORMAT");
		payload2.setAlertLocArgs(new String[]{"Jenna", "Frank"});
		service.sendNotification(token, payload2);
		
		// get feedback
		List<Feedback> list = service.getFeedbacks();
		if (list != null && list.size() > 0) {
			for (Feedback feedback : list) {
				System.out.println(feedback.getDate() + " " + feedback.getToken());
			}
		}
		
		try {
			// sleep 5s.
			Thread.sleep(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// It's a good habit to shutdown what you never use
		service.shutdown();
		
//		System.exit(0);
	}
}

包含推送功能、苹果的feedback功能,很是实用方便。

对于源码,我整理成了工程,下载地址为:http://download.csdn.net/detail/linyu19872008/6934361

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
APNS(Apple Push Notification Service)是苹果公司提供的一种推送服务,在移动应用开发中广泛应用。下面介绍如何使用C#实现APNS消息推送。 首先,需要获取APNS推送证书。具体步骤如下: 1. 登录苹果开发者中心,进入Certificates, Identifiers & Profiles页面。 2. 选择Identifiers选项卡,然后选择App IDs选项,找到你要推送的App ID。 3. 点击Edit按钮,然后勾选Push Notifications选项,保存。 4. 选择Certificates选项卡,点击左下角的“+”按钮,选择“Apple Push Notification service SSL (Sandbox & Production)”选项,按照提示生成证书。 5. 下载证书并安装到电脑中。 接下来,使用第三方库完成APNS消息推送。常用的库有apns-sharp、PushSharp等。这里以PushSharp为例。 1. 安装PushSharp库。可以通过NuGet安装,也可以从GitHub上下载源码编译。 2. 创建推送服务对象。代码如下: ``` ApnsConfiguration config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, "your_certificate_path", "your_certificate_password"); ApnsService service = new ApnsService(config); ``` 其中,第一个参数是环境类型,可以选择生产环境或者测试环境;第二个参数是证书路径;第三个参数是证书密码。 3. 创建推送消息对象。代码如下: ``` ApnsNotification notification = new ApnsNotification(DEVICE_TOKEN, new ApnsPayload("Hello, world!")); ``` 其中,第一个参数是设备的token;第二个参数是消息的内容。 4. 发送推送消息。代码如下: ``` service.QueueNotification(notification); service.Stop(); ``` 其中,QueueNotification方法用于将推送消息加入发送队列;Stop方法用于停止推送服务。 完整代码示例: ``` using PushSharp; using PushSharp.Apple; using System; namespace APNSDemo { class Program { static void Main(string[] args) { ApnsConfiguration config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, "your_certificate_path", "your_certificate_password"); ApnsService service = new ApnsService(config); string DEVICE_TOKEN = "your_device_token"; ApnsNotification notification = new ApnsNotification(DEVICE_TOKEN, new ApnsPayload("Hello, world!")); service.QueueNotification(notification); service.Stop(); Console.WriteLine("Push notification sent."); Console.ReadLine(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

linyu19872008

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

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

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

打赏作者

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

抵扣说明:

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

余额充值