简体   繁体   English

PubNub iOS应用未收到推送通知

[英]PubNub iOS app doesn't receive push notifications

I'm on the latest, trial version of PubNub on iOS 8+, Xcode 7.3, trying to build a chat app. 我正在使用iOS 8+上的PubNub的最新试用版Xcode 7.3,试图构建一个聊天应用程序。 I'm evaluating PubNub as an alternative to another chat server. 我正在评估PubNub,以替代另一个聊天服务器。

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. 我已经按照PubNub文档中有关Apple Push Notifications的说明进行操作,但是我的应用程序在后台运行时从未收到过Push Notifications。

I've created the p12 certificate and imported it into my PubNub keyset. 我已经创建了p12证书并将其导入到我的PubNub密钥集中。 I've enabled push notifications in my Xcode General settings. 我已在Xcode常规设置中启用了推送通知。 I've written the Swift code as specified in the PubNub docs. 我已经按照PubNub文档中的指定编写了Swift代码。 I'm able to publish and subscribe successfully, but my application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) method shows me a 'nil' token. 我能够成功发布和订阅,但是我的应用程序(应用程序:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData)方法向我显示了“零”令牌。

@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. 打印方法告诉我deviceToken为nil。

Any idea what I'm doing wrong? 知道我在做什么错吗? Thanks in advance. 提前致谢。

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. 设备令牌是二进制的,无法使用NSUTF8StringEncoding编码转换为字符串。 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. 您可以使用断点来验证其中的值,但是成功委托本身的调用可以证明您的应用程序从Apple获得了适当的设备推送令牌。

To receive push notifications, you need to use proper publish methods which allow you to specify APNS payload. 要接收推送通知,您需要使用正确的发布方法,该方法允许您指定APNS有效负载。 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). 这是一种方法: https : //www.pubnub.com/docs/swift/api-reference#publish_arg_6有效负载应设置为有效的APNS有效负载字典(根据Apple规范)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM