0
votes

I'm on the latest, trial version of PubNub on iOS 8+, Xcode 7.3, trying to build a chat app. I'm evaluating PubNub as an alternative to another chat server.

I've followed the instructions in the PubNub docs about Apple Push Notifications, but my app never receives push notifications when its in the background.

I've created the p12 certificate and imported it into my PubNub keyset. I've enabled push notifications in my Xcode General settings. I've written the Swift code as specified in the PubNub docs. I'm able to publish and subscribe successfully, but my application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) method shows me a 'nil' token.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PNObjectEventListener {
    var window: UIWindow?
    // Instance property
    var client: PubNub?

    // For demo purposes the initialization is done in the init function so that
    // the PubNub client is instantiated before it is used.
    override init() {

        // Instantiate configuration instance.
        let configuration = PNConfiguration(publishKey: "mypubkey", subscribeKey: "mysubkey")
        // Instantiate PubNub client.
        client = PubNub.clientWithConfiguration(configuration)
        super.init()
        client?.addListener(self)
    }

and:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.client?.subscribeToChannels(["my_channel"], withPresence: true)

let types: UIUserNotificationType = [.Badge, .Sound, .Alert]
let mySettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes:types, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
UIApplication.sharedApplication().registerForRemoteNotifications()

return true
}

And in my push notification registration methods:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    NSUserDefaults.standardUserDefaults().setObject(deviceToken, forKey: "DeviceToken")
    NSUserDefaults.standardUserDefaults().synchronize()
    print("Device Token=\(NSString(data: deviceToken, encoding:NSUTF8StringEncoding))")
    self.client?.addPushNotificationsOnChannels(["my_channel"],
       withDevicePushToken: deviceToken,
       andCompletion: { (status) -> Void in
    ...
    })
}

The print method shows me that the deviceToken is nil.

Any idea what I'm doing wrong? Thanks in advance.

1
As discussed on private channel, getting a nil token is not related to PubNub. But if you do get a valid token and you register for channels with PubNub and you still don't get your push notifications, let us know.Craig Conover
Are you testing with a device or a sim when you get the nil token? Are you properly provisioned?Craig Conover
Device tokens are provided by Apple (and used by PubNub for push) but are provided by Apple. If your provisioning is incorrect (or you are testing on a sim) then your device token will be nil. Remember that simulators will never receive a device token because they are not devices. Here's the link to provisioning your app for push notifications in PubNub pubnub.com/docs/swift/… (Apple also has great articles on this)gurooj

1 Answers

1
votes

You don't have any troubles with your registration code as well as with device token. Device token is binary which can't be converted to string with NSUTF8StringEncoding encoding. You can use breakpoint to verify what value is there, but call of success delegate itself is a proof what your application received proper device push token from Apple.

To receive push notifications, you need to use proper publish methods which allow you to specify APNS payload. Here is one of methods: https://www.pubnub.com/docs/swift/api-reference#publish_arg_6 payload should be set to valid APNS payload dictionary (per-Apple specification).