Use Apple Push Notification Service through Java

China☆狼群 提交于 2019-12-02 16:26:12

Just a little tip, in order to convert your received token into a format suitable for registration with javapns, this code will do the trick:

- (NSString *)convertTokenToDeviceID:(NSData *)token {
NSMutableString *deviceID = [NSMutableString string];

// iterate through the bytes and convert to hex
unsigned char *ptr = (unsigned char *)[token bytes];

for (NSInteger i=0; i < 32; ++i) {
    [deviceID appendString:[NSString stringWithFormat:@"%02x", ptr[i]]];
}

return deviceID;

}

As a shameful self-advertising, I encourage to use java-apns library. Your code will look like:

ApnsService service =
     APNS.newService()
     .withCert("/etc/Certificates.p12", "password")
     .withSandboxDestination() // or .withProductionDestination()
     .build();

String payload =
    APNS.newPayload()
    .alertBody("My alert message")
    .badge(45)
    .sound("default")
    .build();

String deviceToken = "f4201f5d8278fe39545349d0868a24a3b60ed732";

log.warn("Sending push notification...");
service.push(deviceToken, payload);
  1. Your Java code looks solid! However, don't forget to close the connection, through PushNotificationManager.closeConnection(). It's important to cleanup after yourself.

    As a side comment, I notice that you are adding the device 'iPhone' but querying for 'Lambo' afterwards. This is an indication of a bug.

  2. The device token shown in the code is incorrect. Device tokens, currently, as 32-bit long value, which gets hexed into 64 characters. I assume that the server is failing silently when pushing the notification to invalid token!

  3. The only way to get the device token is from the app itself. As provided by the Push Notification guide suggests, the iPhone app needs to register for notification upon launch. In the application:didRegisterForRemoteNotificationsWithDeviceToken:, the iPhone needs to send the device token to your java provider server. (For debugging purposes, you can just NSLog the device token and use it; it never changes across runs).

    I would recommend that you create a server in your java provider server to receive device tokens. Set up a ServerSocket to receive connections from the iPhone and their device token (and any additional info you need) and insert the tokens in the database.

sergio

I tried this and I keep getting hanged when sending the notification, and nothing gets sent.

The issue stems from the following function:

         public void sendNotification(Device device, PayLoad payload) 

It seems that the bufferedreader has NULL

           BufferedReader in = 
           new BufferedReader(new InputStreamReader(this.socket.getInputStream() ) );

So when this portion of the code gets hit it just hangs there in endless loop

  logger.debug( "In: [" + in.readLine() + "]" );

This output is [null]

So then right after then the loops get executed:

      while ( ! this.socket.isInputShutdown() ) {
          while( in.ready() ) {
              logger.debug("ready now");
              logger.debug(in.readLine());
              System.out.println( this.socket.getInputStream().read() );
          }
      }

The code enters the first while loop and waits for the BufferedReader in to be ready and just keeps waiting..... ad that is your hanging

JavaPNS was recently updated to 2.0, and fixed ALL reported issues up to the release date. It does fix the issue you are describing, and using the library is MUCH simpler than it ever was (you can push a notification with a single line of code now).

You seem to be missing the token

pushManager.addDevice("iPhone", "f4201f5d8278fe39545349d0868a24a3b60ed732");

Takes id and token check:

https://github.com/o-sam-o/javapns/blob/master/src/javapns/notification/PushNotificationManager.java#L501

The only way to get a token is from the iphone app. A valid token looks something like this: 1d2d6f34 c5028bca c50df5f9 1992c912 ce7deae8 3bbe7da5 447f6a68 cfecdc0e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!