PubNub iOS应用程序不会收到推送通知
问题描述:
我正在尝试构建聊天应用程序的iOS 8+,Xcode 7.3上的PubNub的最新试用版。我正在评估PubNub作为另一个聊天服务器的替代品。PubNub iOS应用程序不会收到推送通知
我已经按照PubNub文档中关于Apple推送通知的说明进行了操作,但是我的应用在后台从未收到推送通知。
我已经创建了p12证书并将其导入到我的PubNub密钥集中。我在我的Xcode常规设置中启用了推送通知。我已经编写了PubNub文档中指定的Swift代码。我能够成功发布和订阅,但我的应用程序(应用程序: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)
}
和:
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
}
在我的推送通知登记方法:
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
...
})
}
的打印方法,让我发现了deviceToken为零。
任何想法我做错了什么? 在此先感谢。
答
您的注册码和设备令牌没有任何问题。设备令牌是二进制的,不能转换为字符串,编码为NSUTF8StringEncoding。您可以使用断点来验证有哪些值,但成功委托本身的调用证明了您的应用程序从Apple获得了适当的设备推送令牌。
要接收推送通知,您需要使用适当的发布方法,它允许你指定APNS有效载荷。这里有一种方法:https://www.pubnub.com/docs/swift/api-reference#publish_arg_6有效载荷应设置为有效APNS有效载荷字典(按Apple规范)。
正如私人频道所讨论的,获取一个零标记与PubNub无关。但是,如果您获得了有效的令牌,并且您注册了PubNub的频道,但您仍然无法获得推送通知,请告诉我们。 –
当你得到零标记时,你正在测试一个设备还是一个SIM卡?你是否正确配置? –
设备令牌由Apple提供(并由PubNub用于推送),但由Apple提供。如果您的配置不正确(或者您正在测试一个SIM卡),那么您的设备令牌将为零。请记住,模拟器永远不会收到设备令牌,因为它们不是设备。 以下是在PubNub中为您的应用程序配置推送通知的链接https://www.pubnub.com/docs/swift/mobile-gateway#Retrieving_your_mobile_device_IDs(苹果公司也有很棒的文章) – gurooj