JAVAPNS 2.2 成功推送但未在设备上接收

Posted

技术标签:

【中文标题】JAVAPNS 2.2 成功推送但未在设备上接收【英文标题】:JAVAPNS 2.2 success push but not receive on device 【发布时间】:2012-07-05 10:20:11 【问题描述】:

在此处输入代码我尝试使用 JAVAPNS 2.2 从 Java 推送

这是我的代码

import java.util.*;
import org.apache.log4j.BasicConfigurator;
import org.json.JSONException;
import javapns.*;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
import javapns.notification.ResponsePacket;



public class Test 

/**
 * @param args
 */
public static void main(String[] args) 

    BasicConfigurator.configure();

    try 

        PushNotificationPayload payload = PushNotificationPayload.complex();

        payload.addAlert("Hello World");
        payload.addBadge(1);
        payload.addSound("default");
        payload.addCustomDictionary("id", "1");


        System.out.println(payload.toString());
        List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\keystore1.p12", "123456", true, "AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D");

        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());
                    
            
      


     catch (CommunicationException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
     catch (KeystoreException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
     catch (JSONException e) 
        // TODO Auto-generated catch block
        e.printStackTrace();
    


库已成功发送,但我的设备从未收到推送。我已经尝试了我的设备的许多令牌,甚至尝试重新生成证书 .12 但仍然没有用。

日志

0 [main] DEBUG javapns.notification.Payload  - Adding alert [Hello World]

1 [main] DEBUG javapns.notification.Payload  - Adding badge [1]

1 [main] DEBUG javapns.notification.Payload  - Adding sound [default]

1 [main] DEBUG javapns.notification.Payload  - Adding custom Dictionary [id] = [1]

"id":"1","aps":"sound":"default","alert":"Hello World","badge":1

292 [main] DEBUG javapns.communication.ConnectionToAppleServer  - Creating SSLSocketFactory

341 [main] DEBUG javapns.communication.ConnectionToAppleServer  - Creating SSLSocket to gateway.push.apple.com:2195

972 [main] DEBUG javapns.notification.PushNotificationManager  - Initialized Connection to 
Host: [gateway.push.apple.com] Port: [2195]: 1923e91b[SSL_NULL_WITH_NULL_NULL: Socket[addr=gateway.push.apple.com/17.149.35.173,port=2195,localport=65225]]

974 [main] DEBUG javapns.notification.PushNotificationManager  - Building Raw message from deviceToken and payload

974 [main] DEBUG javapns.notification.PushNotificationManager  - Built raw message ID 1 of total length 113

974 [main] DEBUG javapns.notification.PushNotificationManager  - Attempting to send notification: "id":"1","aps":"sound":"default","alert":"Hello World","badge":1

974 [main] DEBUG javapns.notification.PushNotificationManager  -   to device: AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D

1743 [main] DEBUG javapns.notification.PushNotificationManager  - Flushing

1743 [main] DEBUG javapns.notification.PushNotificationManager  - At this point, the entire 113-bytes message has been streamed out successfully through the SSL connection

1743 [main] DEBUG javapns.notification.PushNotificationManager  - Notification sent on first attempt

1743 [main] DEBUG javapns.notification.PushNotificationManager  - Reading responses

1744 [main] DEBUG javapns.notification.PushNotificationManager  - Closing connection
PUSH NOTIFICATION SENT SUCCESSFULLY TO: AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D

有人知道吗?

【问题讨论】:

问题解决,是windows环境的问题。对于 Mac 和 Ubuntu,它工作正常! 你使用网络代理吗? 我在 mac 上,同样的问题,sais 'SENT SUCCESSFULLY' 但我没有在设备上得到任何东西。 @keoaudam 我在 ubuntu 上遇到了同样的问题。你能帮帮我吗?? 【参考方案1】:

这一行应该

List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\keystore1.p12", "123456", true, "AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D");

应该改成这个

List<PushedNotification> NOTIFICATIONS = Push.payload(payload, "D:\\keystore1.p12", "123456", false, "AA67F2F18586D2C83398F9E5E5BE8BA1CD3FF80257CC74BACF2938CE144BA71D");

【讨论】:

以上是关于JAVAPNS 2.2 成功推送但未在设备上接收的主要内容,如果未能解决你的问题,请参考以下文章

推送通知未在多台设备上接收 - 仅在一台设备上获取推送

推送通知未在设备上接收

iOS:使用 JavaPNS 从服务器发送的通知未到达

推送通知未在 iphone 设备中接收

启用声音的 JavaPNS 推送通知

应用程序在设备上的 Beta 版中崩溃,但未在调试中