nodejs + apn 推送给apns服务器的问题
发布于 9 年前 作者 haozxuan 8798 次浏览 最后一次编辑是 8 年前 来自 问答

问题描述:模拟向ios用户发送100条推送消息,后续均正常。但是前10多条显示不正常。具体问题请看下图: untitled1.png 发送模块相关逻辑如下:

	 var apn = require('apn');
	 var notify = new apn.Notification();
	 notify.device = new apn.Device("343e0048d877589f1e8956e2901d7beab575323724c4d719555d942453ecf3a9");
	 notify.payload = {'messageFrom': 'Caroline'};
	 notify.sound = "default";
	 notify.expiry = Math.floor(Date.now() / 1000) + 3;
	
	var apnConnection = new apn.Connection({
		cert            : "cert.pem",
		key             : "key.pem",
		maxConnections  : 100,
		gateway         : "gateway.sandbox.push.apple.com"
	});
	
	var count = 0;
	setInterval(function () {
		if (count === 100) process.kill(process.pid);
		notify.badge = count++;
		notify.alert = "你好 APNS" + count;
		apnConnection.pushNotification(notify, notify.device);
	}, 300);
	
	apnConnection.on("connected", function (openSockets) {
		console.log('[@__](/user/__)@!connected is ', openSockets);
	});
	
	apnConnection.on("disconnected", function (openSockets) {
		console.log('$__$!disconnected is ', openSockets);
	});
	
	apnConnection.on("completed", function () {
		console.log('#__#! completed.');
	});

主要两个问题点: 1、最快发送频率?(本人实验间隔300ms以下,客户端没有反应); 2、对于批量发送是否只能发送内容一样的;

如有相关经验的,希望不吝赐教:)

10 回复

看来并没有很多同学涉猎推送相关,那我就抛砖引玉下,经过摸爬滚打算是总结出一点结论: 首先对于第一个问题,java语音(目前使用的推送是由java写的)推送给apns的策略是sleep 1到3s为了防止出现多条重复的问题。但对于nodejs来说setImmediate函数就可以轻松解决。(下有逻辑贴出) 对于第二个问题,java的处理方式则是一条条的发送,不批量。那我也就学习下,无论是推群组还是个人都是一条条的发送。这样问题就解决了。当然也出现了一些其他的问题,比如之前推送500条给一个设备,设备都能收到。但是现在却只收到200条左右,难道真的印证了苹果的“概不负责”原则,目前还不得而知。

	apnConnection.on("completed", function () {
		console.log('#__#! completed.');
		setImmediate(function () {
			if (count === 500) {
				console.timeEnd('push ios cost time');
				process.kill(process.pid);
			}
			notify.badge = count++;
			notify.alert = "你好 APNS" + count;
			apnConnection.pushNotification(notify, notify.device);
		});
	});

apn 是什么?没听说过。能用于非ios的应用?

@leapon 第三方实现的apple push notification

@haozxuan apn默认不保证推送成功,要用feedback service

@leapon apns是Apple Push Notification Service的简称,对于nodejs来说,已经有一个apn包封装了一些方法用于向apns推送通知。 apns顾名思义只针对apple,对于安卓等非ios设备不能使用此方法。

@openroc 恩,当然。这点还是有所了解的。但是feedback只会返回发送失败的信息,但是对于正常发送出去但是设备收不到的情况就没有返回了,使用上述列子向同一设备推送500条通知,会发现设备只收到了200条左右,一些列异常事件(err、socketError、transmissionError、feedback)都没有异常捕获。猜想是否不能对同一设备在短时间内推送过多数据呢?(ps,设备有限所以才向单一设备推送大量数据以测试性能)

@haozxuan 你超时设置多长?

@haozxuan 关于quality,貌似没有频率限制。如果你在国内推,GFW是否有影响,

Apple Push Notification service includes a default Quality of Service (QoS) component that performs a store-and-forward function.

If APNs attempts to deliver a notification but the device is offline, the notification is stored for a limited period of time, and delivered to the device when it becomes available.

Only one recent notification for a particular app is stored. If multiple notifications are sent while the device is offline, each new notification causes the prior notification to be discarded. This behavior of keeping only the newest notification is referred to as coalescing notifications.

If the device remains offline for a long time, any notifications that were being stored for it are discarded.

@openroc 对于那个超时我不是很理解,不过还是按照列子上写了1小时。

@openroc 这个我理解的是当推送给一个ios设备,但这个设备很久很久都没有联网,或者关机的情况下,apns是会对推送数据销毁的,不过我是在短时间内推送给一个设备很多条,应该不会是这个问题。

回到顶部