1.开启push log
javapns使用的log4j,为确保log的正常工作,在使用过程中添加如下代码:
import org.apache.log4j.*; ... try { BasicConfigurator.configure(); ... } catch (Exception e) { //do sth. }
log4j.properties中添加:
log4j.logger.javapns=debug
2
.sandbox(开发环境)到production(产品环境)迁移的注意事项
两套环境的device tokens是不同的,在迁移后需要更新device tokens。
两套环境的certificates也是不同的,需要更新证书。
修复sandbox环境工作,production推送失败的解决方案。
http:
//www.techjini.com/blog/how-we-fixed-production-push-notifications-not-working-while-sandbox-works/
3
.定制消息内容
public void send (List<Device> devices, Object keystore, String password, boolean production) { /* Build a blank payload to customize */ PushNotificationPayload payload = PushNotificationPayload.complex(); /* Customize the payload */ payload.addAlert("Hello World!"); //apple提示消息 payload.addCustomDictionary("webview", "http://www.womai.com"); //隐藏参数,用于实现一些定制操作,比如自动跳转。 payload.addCustomDictionary("mykey2", 2); // etc. /* Push your custom payload */ List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, devices); }
4.多线程批量发送推送消息
public void send (List<Device> devices, Object keystore, String password, boolean production) { /* Prepare a simple payload to push */ PushNotificationPayload payload = PushNotificationPayload.alert("Hello World!"); /* Decide how many threads you want to create and use */ int threads = 30; /* Start threads, wait for them, and get a list of all pushed notifications */ List<PushedNotification> notifications = Push.payload(payload, keystore, password, production, threads, devices); }
备注:以上多线程方法在所有线程执行完后返回,如果不想等线程执行完毕,可以通过在内部创建一个单独的线程:
(示例: new Thread() {public void run() {...}}.start();).
5.创建消息队列(连接池)
一个队列就是一组连接APNS的多线程连接,队列会动态将消息分发到不同的线程中去。
public void send (String token, Object keystore, String password, boolean production) { /* Prepare a simple payload to push */ PushNotificationPayload payload = PushNotificationPayload.alert("Hello World!"); /* Decide how many threads you want your queue to use */ int threads = 30; /* Create the queue */ PushQueue queue = Push.queue(keystore, password, production, threads); /* Start the queue (all threads and connections and initiated) */ queue.start(); /* Add a notification for the queue to push */ queue.add(payload, token); }
备注:如果不通过queue.start消息队列,队列会在第一次调用queue.add 的时候启动
5.建议开启EnhancedNotificationFormat
默认为开启状态,建议开启,开启后可以通过Payload对象获取到更多推送消息的详细信息,例如执行状态,失效时间等。
6.推送状态(错误)管理
1)error-response packets
pushedNotification.isSuccessful() //判断是否推送成功
pushedNotification.getException() //获取详细错误信息,系统中错误不会抛出,以保证多线程的正常运作,错误信息会记录到pushedNotification中。
示例代码:
List<PushedNotification> notifications = Push.alert("Hello World!", "keystore.p12", "keystore_password", false, devices); for (PushedNotification notification : notifications) { if (notification.isSuccessful()) { /* Apple accepted the notification and should deliver it */ System.out.println("Push notification sent successfully to: " + notification.getDevice().getToken()); /* Still need to query the Feedback Service regularly */ } else { String invalidToken = notification.getDevice().getToken(); /* Add code here to remove invalidToken from your database */ /* Find out more about what the problem was */ Exception theProblem = notification.getException(); theProblem.printStackTrace(); /* If the problem was an error-response packet returned by Apple, get it ResponsePacket theErrorResponse = notification.getResponse(); if (theErrorResponse != null) { System.out.println(theErrorResponse.getMessage()); } } }
2)Feedback Service
public class FeedbackTest {
public static void main(String[] args) {
List<Device> inactiveDevices = Push.feedback("keystore.p12", "keystore_password", false); /* remove inactive devices from your own list of devices */
}
备注:Sandbox Feedback Service not listing device after app is removed
Delays involving the Feedback service
相关推荐
在iOS平台上,推送通知是一种非常...总的来说,基于Java的iOS推送通知涉及到了iOS的通知机制、JavaPNS库的使用以及与APNS服务器的交互。正确配置和使用这些组件,可以有效地实现在Java服务端向iOS设备发送推送通知。
如果用户同意,应用程序将通过与APNS交互获取一个唯一的`deviceToken`,这个token是用于标识设备的,后续推送消息会基于此token。 2. **获取deviceToken**: 应用程序接收到`deviceToken`后,需要将其发送到服务端。...
标题中的“jsp iOS推送的包”指的是一个针对iOS设备进行远程通知推送的开发资源包,主要基于Java平台。这个包包含了一系列的库文件,用于帮助开发者实现从服务器端向iOS客户端发送Push Notification服务。在iOS应用...
总的来说,iOS消息推送服务端开发涉及证书配置、连接APNs、构造推送消息以及错误处理等多个环节。使用JavaAPNS这样的库可以简化这一过程,特别是其多线程支持,能够提高推送效率。然而,开发者还需要根据实际需求...
在这个Java推送iOS的示例中,我们可能用到了诸如JavaPNS、Apns4j等开源库,这些库简化了与APNs接口的通信。它们支持HTTP/2协议,这是APNs推荐的通信方式,提供了更高的效率和可靠性。 1. **JavaPNS库的使用**: ...
Bouncy Castle处理加密安全,Commons IO和Lang提供通用的工具函数,Log4j提供日志服务,JavaPNS则负责iOS设备的推送通知,而Commons Logging则作为日志适配器,使得更换日志实现变得更加简单。了解并熟练使用这些库...