tvOS 中的 Apple 推送通知
Apple Push Notifications in tvOS
大家好,我是 tvOS 的新手。我有一个为 APNS 注册的电视应用程序。
但是当我推送通知时,我无法收到通知。
我收到了设备令牌,但没有收到通知。
虽然我尝试使用移动设备收到通知,但在 tvOS 中却没有收到通知,为什么会这样...?
我该如何解决这个问题..?
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted == true
{
print("Allow")
UIApplication.shared.registerForRemoteNotifications()
}
else
{
print("Don't Allow")
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {[=10=] + String(format: "%02X", )})
print("DEVICE TOKEN = \(deviceTokenString)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print(userInfo)
}
tvOS 仅支持 2 种类型的通知:badges
和 content-available
。所以你需要将这两种类型中的一种发送到 APNS。这些类型的通知中的任何一种都只会更改应用程序图标上的徽章编号。当您打开应用程序时,只有最新的通知才会发送到您的应用程序。 iOS 上的通知没有视觉呈现
它的外观参见 WWDC 2016/Session 206/tvOS, start watching from 21:20
的演示文稿
更新:
在 tvOS 11 上出现 Silent notifications
唤醒应用程序并允许在后台刷新内容
这是我的 tvOS 通知解决方案。
在 AppDelegate 中
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// set self (AppDelegate) to handle notification
UNUserNotificationCenter.current().delegate = self
// Request permission from user to send notification
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { authorized, error in
if authorized {
DispatchQueue.main.async(execute: {
application.registerForRemoteNotifications()
})
}
})
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
//print(userInfo)
print("Notification Received")
let nc = NotificationCenter.default
nc.post(name: Notification.Name("foo"), object: nil)
}
第一个函数提供通知所需的权限。
第二个函数收到通知并向当前 viewcontroller 发送通知并使魔法发生。
这是viewcontroller
//viewload NotificationCenter.default.addObserver(self, selector: #selector(updateTable(_ :)), name: Notification.Name("foo"), object: nil)
大家好,我是 tvOS 的新手。我有一个为 APNS 注册的电视应用程序。
但是当我推送通知时,我无法收到通知。 我收到了设备令牌,但没有收到通知。
虽然我尝试使用移动设备收到通知,但在 tvOS 中却没有收到通知,为什么会这样...?
我该如何解决这个问题..?
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted == true
{
print("Allow")
UIApplication.shared.registerForRemoteNotifications()
}
else
{
print("Don't Allow")
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {[=10=] + String(format: "%02X", )})
print("DEVICE TOKEN = \(deviceTokenString)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print(userInfo)
}
tvOS 仅支持 2 种类型的通知:badges
和 content-available
。所以你需要将这两种类型中的一种发送到 APNS。这些类型的通知中的任何一种都只会更改应用程序图标上的徽章编号。当您打开应用程序时,只有最新的通知才会发送到您的应用程序。 iOS 上的通知没有视觉呈现
它的外观参见 WWDC 2016/Session 206/tvOS, start watching from 21:20
更新:
在 tvOS 11 上出现 Silent notifications
唤醒应用程序并允许在后台刷新内容
这是我的 tvOS 通知解决方案。
在 AppDelegate 中
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// set self (AppDelegate) to handle notification
UNUserNotificationCenter.current().delegate = self
// Request permission from user to send notification
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: { authorized, error in
if authorized {
DispatchQueue.main.async(execute: {
application.registerForRemoteNotifications()
})
}
})
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
//print(userInfo)
print("Notification Received")
let nc = NotificationCenter.default
nc.post(name: Notification.Name("foo"), object: nil)
}
第一个函数提供通知所需的权限。
第二个函数收到通知并向当前 viewcontroller 发送通知并使魔法发生。
这是viewcontroller
//viewload NotificationCenter.default.addObserver(self, selector: #selector(updateTable(_ :)), name: Notification.Name("foo"), object: nil)