0

I am trying to create a PushNotification app and I am working with PubNub. I have gone through all of the necessary steps to:

  • Include PubNub libraries on my workspace
  • Create the proper provisioning profile for the App
  • Ensured PushNotification was in the profile
  • Create the proper SSL certificate for the app
  • Add the certificate to the keychain access
  • Create the proper .pem file
  • Uploaded the file to PubNub
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(     NSDictionary *)launchOptions
{
    // #2 Register client for push notifications
    NSLog(@" Options set");

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:    (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    [PubNub setDelegate:self];
    return YES;
}

After getting some initial compile errors, I was able run the app on my device. When I ran the app on my device, I did not receive the "...Would Like To Send Yo Push Notifications" message as I thought I should. I am able to use the PubNun console to send a message and I see the messages are received via the log, but I do not get any notification or banner. I placed an NSLog in the didFinishLaunchingWithOptions method and I see the display in the log.

After that, I deleted the app from the device, deleted the provisioning profile form the device, restated Xcode, and compile and an again with the same results. Not sure what to try next.

modusCell
  • 13,151
  • 9
  • 53
  • 80
Kevin McFadden
  • 337
  • 1
  • 5
  • 23

2 Answers2

2

EDIT: The specific problem you're running into has to do with the fact that you are running the application through Xcode, and in a nutshell you can't test production push notifications this way, you'd have to use the sandbox push server and certs. If you want to test prod pushes you'll need to distribute the application signed with an Ad Hoc provisioning profile. I'm sure there are a million ways to do this, but actually my two favorites are using Testflight and Crashlytics' new Beta feature, that allows you to distribute ad hoc builds to devices basically using a single line of code, automatically once you archive a new build. I would recommend Crashlytics because it also automatically handles extremely useful analytics as well as crash reporting.

If you are referring to the initial push notifications permissions pop-up, that will only be displayed once or the lifetime of the application. The ONLY way I believe to get around this is to uninstall the up and leave it uninstalled for 3 days (I believe this is the limit).

I'm unfortunately not familiar with PubNub, but in your application are you registering for push notifications and receiving a device token?

Mike
  • 9,765
  • 5
  • 34
  • 59
  • Yes. I am receiving the token. I am seeing all of the proper messages in the logs. The other thing thats funny is that the "didReceiveRemoteNotification" is not geeing the alert when I send the message from the PubNub console. It never reaches this code for some reason as well, I have this code in the AppDelegate.m. – Kevin McFadden Aug 11 '14 at 01:54
  • How are you running the app on the device, plugged into xcode? – Mike Aug 11 '14 at 01:56
  • Yes. The device is plugged into Xcode. I have tried running the app with the device disconnected as well. PubNub console shows success but nothing on the device. – Kevin McFadden Aug 11 '14 at 01:57
  • That's your problem, I'll update the answer in a bit - but in a nutshell you can't get push notifications when the app is running through xcode, you need to test it on an ad hoc build. – Mike Aug 11 '14 at 01:58
  • Created an AdHoc provision profile in the using the development profile and the app ID and my device. Still no luck. I will try it with the Crashlytics product to see if I am forgetting something else in the profile or the implementation. – Kevin McFadden Aug 11 '14 at 02:48
  • Even with an AdHoc profile you can't get push notifications when connected to Xcode. You are given a device token, but it is not a valid token and cannot be used for push notifications. When you run it in a true ad hoc build separate from Xcode you will be given a totally new and valid token and pushes should work from there. – Mike Aug 11 '14 at 03:16
  • So are you saying that I should not install the profile using Xcode? I will try that approach. – Kevin McFadden Aug 11 '14 at 13:39
  • https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/AppDistributionGuide/TestingYouriOSApp/TestingYouriOSApp.html – Mike Aug 11 '14 at 14:46
  • @Mike it is not true as for inability to test push notifications with connected Xcode to device. iPhone Simulator can't be used for this purpose, but device can and w/o any limitations from whether it connected to Xcode debugger or not. – Serhii Mamontov Aug 18 '14 at 14:38
  • Perhaps I was unclear, I mean when the app is running from Xcode, not simply connected to it. – Mike Aug 18 '14 at 15:33
  • @Mike and still, if it is running on device, push notifications will work. Why they shouldn't? How Xcode can prevent APNS to send push notification on device? Xcode doesn't limit device capabilities. – Serhii Mamontov Aug 19 '14 at 07:37
  • You're correct - I should clarify that you can't test push notifications in the simulator, and if you want to test push notifications when building from xcode you need to setup your server to use the sandbox APNS server, otherwise you'll need to build it ad-hoc and connect to the prod APNS server. – Mike Jun 30 '15 at 02:01
1

Kevin,

If this is all what you try to do, to receive notifications using PubNub service, than it is not enough. After you receive device push notification token, you need to pass it to PubNub client library, for example like this with default configuration:

- (BOOL)            application:(UIApplication *)application 
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Initialise PubNub client.
    // Warning, you should use pub/sub/secret keys for your account (at admin.pubnub.com) to 
    // which you uploaded your development push notification certificate (in .pem file).
    [PubNub setupWithConfiguration:[PNConfiguration defaultConfiguration] andDelegate:self];
    [PubNub connect];

    UIRemoteNotificationType type = (UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound);
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:type];

    return YES;
}

- (void)                               application:(UIApplication *)application 
  didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    [PubNub enablePushNotificationsOnChannel:[PNChannel channelWithName:@"my-test-channe-for-push-notifications"] 
                         withDevicePushToken:deviceToken];
}

// Observe for some PubNub delegate methods related to push notification observation (or use completion block enabled version of API):
- (void)pubnubClient:(PubNub *)client didEnablePushNotificationsOnChannels:(NSArray *)channels {

    NSLog(@"PubNub client enabled push notifications on channels: %@", channels);
}

- (void)pubnubClient:(PubNub *)client pushNotificationEnableDidFailWithError:(PNError *)error {

    NSLog(@"PubNub client failed push notification enable because of error: %@", error);
}

If you see that push notification enabling has been successful for you, then use http://pubnub.com/console with pub/sub/secret keys from your account at http://admin.pubnub.com to send message into the channel (in our case "aps" payload should be sent to the channel with "my-test-channe-for-push-notifications" name).

Serhii Mamontov
  • 4,942
  • 22
  • 26
  • You're a life saver. No where did I see in all of the guides that there's a method you had to run called enablePushNotifications. – micnguyen Jul 21 '15 at 04:14