【发布时间】:2016-04-18 18:36:21
【问题描述】:
我正在关注 this article 和 M Penades' answer 构建 VoIP 通知演示。我可以使用didRegisterUserNotificationSettings 注册通知,并且可以从didUpdatePushCredentials 获取voip 令牌。
但是当我使用 houston 模拟向我的设备发送通知时,我可以从控制台收到 1 push notification sent successfully 消息,但在设备端没有任何操作或日志。似乎从未调用过didReceiveIncomingPushWithPayload。
附:我正在使用 Xcode 7.3、iPhone 6(iOS 9.3) 和 iPad Mini(iOS 9.3) 进行开发。用于安装的分布式 Ad Hoc ipa。
这里发布的代码。
import UIKit
import PushKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let voipRegistry = PKPushRegistry(queue: dispatch_get_main_queue())
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Enable all notification type. VoIP Notifications don't present a UI but we will use this to show local nofications later
let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound] , categories: nil)
//register the notification settings
application.registerUserNotificationSettings(notificationSettings)
//output what state the app is in. This will be used to see when the app is started in the background
NSLog("app launched with state \(application.applicationState.stringValue)")
return true
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
//output to see when we terminate the app
NSLog("app terminated")
}
}
extension AppDelegate {
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
//register for voip notifications
NSLog("didRegisterUserNotificationSettings called")
voipRegistry.desiredPushTypes = Set([PKPushTypeVoIP])
voipRegistry.delegate = self;
}
}
extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
NSLog("didUpdatePushCredentials called")
//print out the VoIP token. We will use this to test the nofications.
NSLog("voip token: \(credentials.token)")
}
func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
NSLog("didReceiveIncomingPushWithPayload called")
let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String>
let message = payloadDict?["alert"]
//present a local notifcation to visually see when we are recieving a VoIP Notification
if UIApplication.sharedApplication().applicationState == UIApplicationState.Background {
NSLog("incoming notificaiton from background")
let localNotification = UILocalNotification();
localNotification.alertBody = message
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
UIApplication.sharedApplication().presentLocalNotificationNow(localNotification);
}
else {
NSLog("incoming notificaiton from frontend")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let alertController = UIAlertController(title: "Title", message: "This is UIAlertController default", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
})
}
NSLog("incoming voip notfication: \(payload.dictionaryPayload)")
}
func pushRegistry(registry: PKPushRegistry!, didInvalidatePushTokenForType type: String!) {
NSLog("didInvalidatePushTokenForType called")
NSLog("token invalidated")
}
}
extension UIApplicationState {
//help to output a string instead of an enum number
var stringValue : String {
get {
switch(self) {
case .Active:
return "Active"
case .Inactive:
return "Inactive"
case .Background:
return "Background"
}
}
}
}
知道如何接收此类通知吗?
【问题讨论】:
-
我遇到了同样的问题,当我尝试使用 node-apn 发送通知时收到一条错误消息,似乎 houston 控制台行工具在某些情况下不显示错误。还要确保您使用的是正确的标识符和证书。
-
您终于在设备上收到通知了吗?我需要对上面发布的本机 swift 代码进行任何更改吗?谢谢。
-
在我的情况下,我试图发送带有错误 .pem 文件的通知,所以是的,这已得到修复。另请注意,设备令牌对于开发和分发是不同的,这在您的情况下也可能是一个问题。如果我没有遗漏任何内容,代码看起来还不错。
-
谢谢!你救了我的命。我将服务器地址从
gateway.sandbox.push.apple.com(用于开发)更改为gateway.push.apple.com(用于生产,我认为对于VoIP Push,这是唯一的选择)。终于收到推送消息了。