【发布时间】:2017-04-14 07:48:34
【问题描述】:
使用 swift 3.1 在 Xcode8.3 上运行它
下面是我在 AppDelegate.swift 中的代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
if granted {
print("OK!")
}
else {
print("No!")
}
})
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("==== didReceiveRemoteNotification ====")
print(userInfo)
}
我使用 node-apns 将通知推送到我的应用程序,我可以从 Xcode 中的调试区域收到消息。
==== didReceiveRemoteNotification ====
[AnyHashable("id"): 123, AnyHashable("aps"): {
alert = "Hello World \U270c";
badge = 3;
}]
但是,我的 iPhone 没有收到任何通知。
我错过了什么吗?
【问题讨论】:
-
您不需要代码来将设备标识符上传到您的任何地方的服务吗?
-
可能是因为您的应用位于
foreground中?只有在off或background时才会显示推送 -
@JuicyFruit 谢谢。之后,我在手机的后台运行我的应用程序。我可以收到通知。如何设置为前台和后台都可以接收通知。
-
@Dreams 检查我的答案