0

I am completely new to the Objective-C language. All I need to implement is the Push Notifications bit on my app for now. I have written the Client side on XCode 6 and the server side in Java using the javapns library. Now, while the server manages to send the notification (I get the confirmation message), I receive nothing on my device, be it the app is active or running in background.

Can somebody please direct me in the right direction? Thank you!

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];

return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken{
//Store the Device Token
NSLog(@"%@", newDeviceToken);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Failed to register with error: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Push received: %@", userInfo);
}

Server side:

public class PushServer {
public static void main(String[] args) {
    try {
        BasicConfigurator.configure();
        Push.alert("Message!", "***.p12", "***", false,
                "92ab*************91af4");
    } catch (CommunicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeystoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

This is the output I receive when I try to send the notification:

0 [main] DEBUG javapns.notification.Payload  - Adding alert [Message!]
210 [main] DEBUG javapns.communication.ConnectionToAppleServer  - Creating SSLSocketFactory
229 [main] DEBUG javapns.communication.ConnectionToAppleServer  - Creating SSLSocket to gateway.sandbox.push.apple.com:2195
1077 [main] DEBUG javapns.notification.PushNotificationManager  - Initialized Connection to Host: [gateway.sandbox.push.apple.com] Port: [2195]: 735b478[SSL_NULL_WITH_NULL_NULL: Socket[addr=gateway.sandbox.push.apple.com/17.172.232.46,port=2195,localport=53762]]
1079 [main] DEBUG javapns.notification.PushNotificationManager  - Building Raw message from deviceToken and payload
1080 [main] DEBUG javapns.notification.PushNotificationManager  - Built raw message ID 1 of total length 73
1080 [main] DEBUG javapns.notification.PushNotificationManager  - Attempting to send notification: {"aps":{"alert":"Message!"}}
1080 [main] DEBUG javapns.notification.PushNotificationManager  -   to device: 92a**********1af4
2327 [main] DEBUG javapns.notification.PushNotificationManager  - Flushing
2327 [main] DEBUG javapns.notification.PushNotificationManager  - At this point, the entire 73-bytes message has been streamed out successfully through the SSL connection
2327 [main] DEBUG javapns.notification.PushNotificationManager  - Notification sent on first attempt
2327 [main] DEBUG javapns.notification.PushNotificationManager  - Reading responses
2749 [main] DEBUG javapns.notification.PushNotificationManager  - Found 0 notifications that must be re-sent
2749 [main] DEBUG javapns.notification.PushNotificationManager  - No notifications remaining to be resent
2749 [main] DEBUG javapns.notification.PushNotificationManager  - Closing connection

Any help is appreciated.

Somya
  • 13
  • 6
  • Have you enabled push notifications in your provisioning profile? https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/ConfiguringPushNotifications/ConfiguringPushNotifications.html – dstudeba Sep 07 '15 at 07:02
  • Yes, I have. Still no luck. Is there a handler of some sort that I might be missing? – Somya Sep 07 '15 at 08:05
  • I have found Push Notifications can be frustrating to implement and debug. If you have double checked that you are including the proper provisioning file that allows notifications you should try a different device, and/or a 3rd party notifications API such as Parse, Intercom, or Localytics. Also note that your code is for iOS 8. – dstudeba Sep 07 '15 at 14:30
  • Probably not the problem, but see this answer about logging NSData objects. http://stackoverflow.com/questions/6626038/printing-nsdata-using-nslog – dstudeba Sep 07 '15 at 14:31
  • Hey. Thanks for trying to help out. But I don't understand how this link might help me. Also, I'm doing this as part of a project to help me understand Push Notifications better. So I can't use 3rd party services. Although, you're right, they'd be far easier to implement. Can you explain how the NSData objects link might help me though? – Somya Sep 07 '15 at 18:18
  • You have this line, which I do not believe is going to do what you want it to do. `NSLog(@"%@", newDeviceToken);` The link I added explains how to convert an NSData object to a String which you can log. Also if the third party tools work, you will know it is a problem with your code, otherwise it could be a problem with your profile or setup. – dstudeba Sep 07 '15 at 18:24
  • newDeciveToken does get me the device token which tells me that the device is registered. So that bit works just fine. I don't think the didReceiveRemoteNotification method is being called at all. I shall try with a 3rd party and check if it works. – Somya Sep 08 '15 at 04:47

2 Answers2

1

By chance would you happen to be building with Xcode 7 and targeting iOS 9?

If so, you are likely running into the new security default setting which requires accessing URLs securely, as described here:

Disabling ATS for an in-app browser in iOS 9?

Also, you seem to be mixing old methods with new.

registerUserNotificationSettings is an iOS 8 method that replaced registerForRemoteNotificationTypes.

So, only using the former, it looks like you're only targeting iOS 8 and later.

But then you use didReceiveRemoteNotification:, which is the old version of the method, corresponding to registerForRemoteNotificationTypes. But since you're going with the newer versions, you should be using didReceiveRemoteNotification:fetchCompletionHandler.

Community
  • 1
  • 1
  • I am building on XCode 6.4 targeting an iOS 8.3 device. I tried using didReceiveRemoteNotification:fetchCompletionHandler – Somya Sep 11 '15 at 08:14
-1

You need to convert NSData to string and get actual device token by implementing following changes in your method.

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *myToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                              stringByReplacingOccurrencesOfString: @">" withString: @""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];
    // *** Now user `myToken` to send notification ***
    NSLog(@"%@",myToken);
}

Along with it you need to make sure you are running your application with same provisional profile (Developer/Distribution) which is used to send push notification on your server side. both should be same. I hope its clear to you.

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57