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.