ios tvOS中的Apple推送通知

euoag5mw  于 2023-07-01  发布在  iOS
关注(0)|答案(2)|浏览(136)

你好,我是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("", {$0 + String(format: "%02X", $1)})
     print("DEVICE TOKEN = \(deviceTokenString)")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print(error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print(userInfo)
}
nzk0hqpo

nzk0hqpo1#

tvOS仅支持两种类型的通知:badgescontent-available。因此,您需要将这两种类型中的一种发送到APNS。这些类型的通知中的任何一种仅更改应用程序图标上的徽章编号。当您打开应用程序时,只有最新的通知才会发送到您的应用程序。没有像iOS上那样的通知的视觉呈现它在WWDC 2016/Session 206/tvOS, start watching from 21:20的演示中看起来如何

更新:在tvOS 11出现Silent notifications唤醒应用程序,并允许在后台刷新内容

WWDC 2017 watch from 7:50

ztyzrc3y

ztyzrc3y2#

这是我在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发送通知,使魔术发生。
这是视图控制器

//viewload  NotificationCenter.default.addObserver(self, selector: #selector(updateTable(_ :)), name: Notification.Name("foo"), object: nil)

相关问题