`
sillycat
  • 浏览: 2543108 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

APNS(4)Recall the Process and Learn Java APNS

 
阅读更多
APNS(4)Recall the Process and Learn Java APNS
1. Process about the Certificate File
Generate the Certificate Signing Request(CSR)  ---  xxxx.certSigningRequest

Export the p12 private key                                ---  xxxx.p12
Upload my xxxx.certSigningRequest file to Apple and generate the certificate
                                                                      ---  xxxx.cer
Based on the certSigningRequest file and xxxx.p12 and  xxxx.cer I will generate the PEM file
>xxxx.cer -----> xxxx1.pem
>xxxx,p12 -----> xxxx2.pem

Make the PEM file together
xxxx1.pem + xxxx2.pem ----> xxxx.pem

That is the whole process.

2. Process to Deal directly with the p12
Open the keychain Access ---> Request a Certificate Information from Authority Certificate ---> Output: CertificateSigningRequest.certSigningRequest

Open the keychain Access ---> login ----> Keys ---> [Command Name] private key ----> Export to p12 file Output:
[Command Name].p12

Logon to Apple ----> identifiers -----> App IDs ----> Settings ----> Create Certificate ---- Upload my xxxxx.certSigningRequest file
aps_development.cer

That is right. Then based on p12 and CER, I will generate 2 PEM files. And make 2 PEM together into 1. That is done.

3. JavaPNS
Basic Push Notification
Configure the pom.xml to be
<dependency>      <groupId>com.google.code</groupId>      <artifactId>javapns</artifactId>     <version>2.2</version>

</dependency>

And the basic simple implementation is as follow:
package com.sillycat.easyrestserver.main;
import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;

publicclass PushTestMain {
publicstaticvoid main(String[] args) {
     try {           Push.alert(                "Hello World!",                "[file directory and name].p12",                "password", false,                "APNS token from the device");      } catch (CommunicationException e) {           e.printStackTrace();      } catch (KeystoreException e) {           e.printStackTrace();      } } }


Error Message:
04-17 11:20:08 [ERROR] javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:426) - Attempt to send Notification failed and beyond the maximum number of attempts permitted
04-17 11:20:08 [ERROR] javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:429) - Delivery error
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)

at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)

Solution:
>openssl x509 -in aps_development.cer -inform DER -out developer_identity.pem -outform PEM
>openssl pkcs12 -nocerts -in EasyiOSSampleKey.p12 -out mykey.pem
>openssl pkcs12 -export -inkey mykey.pem -in developer_identity.pem -out EasyiOSSampleKey_final.p12

The means do not directly use the p12 from the private key, In my PHP example, I used PEM file based on the CER from apple and p12 from my private key.

Almost the same process to generate the P12, also based on the CER from apple and p12 from my private key.

In the class Push, there are methods alert, badge, sound, combined and etc.

Advanced Push Notification
PushNotificationPayload payload = PushNotificationPayload.complex();
playload.addBadge(int badge)
playload.addSound(String sound)
playload.addAlert(String message)
List<PushedNotification]]> notifications = Push.payload(payload, keystore, password, production, devices);

Send Multiple Message
private static void sendMultiple(){
PushNotificationPayload payload = PushNotificationPayload.complex(); try {      payload.addAlert(DEFAULT_MESSAGE);      payload.addSound("default");      payload.addBadge(1); } catch (JSONException e1) {      e1.printStackTrace(); } int threads = 2; List<Device> devices = new ArrayList<Device>(); try {      devices.add(new BasicDevice(DEFAULT_TOKEN)); } catch (InvalidDeviceTokenFormatException e1) {      e1.printStackTrace(); } try {      List<PushedNotification> notifications = Push.payload(payload,      P12_FILE, DEFAULT_PASSWORD, false, threads, devices);      System.out.println(notifications); } catch (Exception e) {      e.printStackTrace(); }

}

Creating a push queue
PushNotificationPayload payload = PushNotificationPayload.alert("Hello World!");
int threads = 30;
PushQueue queue = Push.queue(keystore, password, production, threads);
queue.start();

queue.add(payload, token);

private static void sendQueue() {
PushNotificationPayload payload = PushNotificationPayload.complex(); try {      payload.addAlert(DEFAULT_MESSAGE);      payload.addSound("default");      payload.addBadge(1); } catch (JSONException e1) {      e1.printStackTrace(); } int threads = 2; PushQueue queue = null; try {      queue = Push.queue(P12_FILE, DEFAULT_PASSWORD, false, threads);      queue.start();      queue.add(payload, DEFAULT_TOKEN); } catch (KeystoreException e) {      e.printStackTrace(); } catch (InvalidDeviceTokenFormatException e) {      e.printStackTrace(); } try {      Thread.sleep(10* 1000); } catch (InterruptedException e) {      e.printStackTrace(); }

}

References:
http://sillycat.iteye.com/blog/1769158
http://sillycat.iteye.com/blog/1769163
http://sillycat.iteye.com/blog/1769781

javaPNS
https://code.google.com/p/javapns/
https://code.google.com/p/javapns/wiki/PushNotificationBasic
https://code.google.com/p/javapns/wiki/PushNotificationAdvanced

http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2

http://stackoverflow.com/questions/12585858/cannot-send-push-notifications-using-javapns-javaapns-ssl-handshake-failure
分享到:
评论

相关推荐

    最新java整合APNS推送服务

    Java整合APNS推送服务是将Java应用程序与Apple Push Notification Service(APNS)相结合,以便能够向iOS和tvOS设备发送即时消息。APNS是苹果公司提供的一个服务,它允许开发者在用户不打开应用的情况下,向他们的...

    JavaAPNS开源库apns4j.zip

    apns4j 是 Apple Push Notification Service 的 Java 实现!Maven:   &lt;groupId&gt;com.github.teaey&lt;/groupId&gt;   &lt;artifactId&gt;apns4j   &lt;version&gt;1.0.1  示例代码:KeyStoreWraper keyStore = ...

    java调用apns推送的实现

    在Java中实现APNs推送,我们可以使用第三方库如`Java-APNS`或`Apns4j`,它们封装了与APNs交互的复杂过程,使代码更加简洁。不过,如果你选择自己实现,需要注意以下几点: - **错误处理**:APNs服务器对推送消息有...

    notnoop-java-apns.zip_com.notnoop.apns_java apns_notnoop apns_no

    在Java开发环境中,当需要构建一个能够向Apple Push Notification Service (APNS)发送推送通知的服务时,`notnoop-java-apns`是一个常用的第三方库。这个压缩包`notnoop-java-apns.zip`包含了开发者用于实现这一功能...

    Java向苹果服务器推送消息(Java实现HTTP/2协议发送APNS)

    4. **构建推送请求**:根据APNs规范创建一个JSON对象,包含必要的通知字段。例如: ```json { "aps": { "alert": { "title": "通知标题", "body": "通知正文" }, "sound": "default" }, "topic": ...

    基于Java语言实现的苹果推送服务APNS4J设计源码

    该项目是一款基于Java语言实现的苹果推送服务APNS客户端库——APNS4J的设计源码,共包含37个文件,主要包括31个Java源文件、1个Git忽略文件、1个YAML配置文件、1个LICENSE文件、1个README文本文件、1个Markdown文件...

    java向苹果apns实现推送所需jar包

    2. **Java APNs库**:描述中的"iphone_apns_send"可能是用于Java的APNs库,如`Apns4j`或`JavaAPNS`。这些库封装了与APNs服务器交互的复杂过程,包括证书处理、错误处理和消息构建。例如,`Apns4j`提供了简单易用的...

    java进行苹果APNS消息推送

    Java进行苹果APNS(Apple Push Notification Service)消息推送是一项在iOS和macOS应用程序中实现远程通知功能的关键技术。本文将详细介绍如何使用Java实现这一过程,同时关注`javapns`库和`bcprov-jdk`加密组件的...

    apns-http2:Java库,用于使用Apple的HTTP2 API通过APNS发送通知

    一个Java库,用于使用Apple的新HTTP / 2 API通过APNS发送通知。 该库使用OkHttp。 以前的版本包括对Jetty客户端的支持,但是,由于Jetty客户端的不稳定,我们已将其删除。 注意:确保引导类路径中包含Jetty的ALPN ...

    ios通知推送实现 java实现 dbay-apns4j-1.0

    利用socket 推送消息到 苹果的服务器。APNS 协议。支持失败重新发送。批量发送等。

    java(后台) ios 推送(APNS) 源码+支持jar包

    java(后台) ios 推送(APNS) 源码+支持jar包,代码经测试没问题,包含4个jar包:javaAPNS2.2、log4j、org.bouncycastle.jce1.39.0、apache.common.lang所属包

    java apns eclipse

    Java APNS Eclipse项目是一个使用Java语言在Eclipse集成开发环境中构建的Apple Push Notification Service(APNs)服务器应用程序。APNs是苹果公司提供的服务,用于向iOS、iPadOS、watchOS和macOS设备发送即时推送...

    java apns 推送示例

    在Java中实现APNS推送,你可以使用开源库,例如`com.notnoop.apns`(Apns4j)或`io.github.relayrides:pushy`。以下是一个基本的使用流程: 1. **导入依赖**:在项目中添加对应的库依赖,比如Pushy。 2. **加载...

    Java_APNS:Java APNS 服务器

    Java APNS,全称为Apple Push Notification service,是苹果公司提供的一项服务,允许应用程序开发者向iOS、iPadOS、watchOS和macOS设备发送实时通知。在Java中实现APNS服务器,可以让我们利用Java强大的编程能力来...

    iphone 推送通知 服务器端java 实现

    4. **构建推送消息**:通知消息应遵循APNs的JSON格式,包括设备令牌、通知标题、正文、声音等。例如: ```json { "aps": { "alert": "Hello, World!", "sound": "default" } } ``` 5. **发送推送**:使用...

    notnoop-java-apns-0.1.4

    "notnoop-java-apns-0.1.4"是一个针对苹果推送通知服务(Apple Push Notification Service,简称APNS)的Java实现库。这个库由notnoop开发者创建,版本号为0.1.4,旨在帮助Java开发者更方便地集成和管理APNS服务,...

    iphone消息推送APNS

    **苹果推送通知服务(Apple Push Notification service,简称APNS)** APNS是苹果公司提供的一项服务,用于向iOS、iPadOS、watchOS、tvOS以及macOS设备推送通知。通过APNS,应用开发者可以在他们的应用不在前台运行...

    ApnsPHP_apnsphp_php_

    APNSPHP是一个专门为苹果推送通知服务(Apple Push Notification service,简称APNs)设计的PHP库。这个库的主要目的是帮助PHP开发者方便地与苹果服务器进行通信,以便向iOS、macOS和watchOS设备发送推送通知。从...

    Apple推送服务(APNs)Java客户端zpush.zip

    Apple APNs java client, based on netty4. 基于netty4实现的苹果通知推送服务Java客户端。 特点: 支持第三版通知推送,即command = 2。目前的绝大部分Java客户端都只支持command = 1,即第二版。 ...

    .net APNS推送

    .NET APNS推送是一个基于.NET框架实现的用于向Apple Push Notification Service (APNS)发送推送通知的项目。APNS是苹果公司提供的服务,允许开发者通过云端将实时消息推送到iOS、iPadOS、watchOS以及macOS设备上的...

Global site tag (gtag.js) - Google Analytics