gpt4 book ai didi

ios - 如何将 Iphone 的远程通知传递给 Watch OS?

转载 作者:行者123 更新时间:2023-11-28 12:15:07 24 4
gpt4 key购买 nike

我正在开发一个非常庞大的应用程序,它已经具有远程推送通知。此通知还包括 Facebook 的通知,它们在 iPhone 和 iPad 上显示得很好,问题是现在我需要在 Apple Watch 上显示相同的通知,但我不知道该怎么做。

这是我的 AppDelegate,我在其中声明远程通知:

  func handleNotification(_ application: UIApplication,userInfo: [AnyHashable: Any]) {
if let notification = userInfo["notification"] as? NSDictionary {

let notiicationInfo = notification
let notiicationAPS = userInfo["aps"] as! NSDictionary
let type = notiicationInfo["type"] as! String!
let name = notiicationInfo["name"] as! String!
let value = notiicationInfo["value"] as! String!
let message = notiicationAPS["alert"] as! String!
NSLog("notiicationInfo \(notiicationInfo)")

var promotions : [[String:Any]]? = []

promotions!.append(["id": name as AnyObject, "name":message as AnyObject, "creative":"" as AnyObject, "position":"1" as AnyObject])

if let customBar = self.window!.rootViewController as? CustomBarViewController {
if (application.applicationState == UIApplicationState.background || application.applicationState == UIApplicationState.inactive)
{
let dataLayer: TAGDataLayer = TAGManager.instance().dataLayer
dataLayer.push(["ecommerce": ""])
dataLayer.push(["ecommerce": NSNull()])
dataLayer.push(["event": "promotionClick", "ecommerce": [ "promoClick" : ["promotions": promotions!] ] ] )

let delay = 2.0 * Double(NSEC_PER_SEC)
let time = DispatchTime.now() + Double(Int64(delay)) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: time, execute: {
customBar.handleNotification(type!,name:name!,value:value!)
})
}

else {
let dataLayer: TAGDataLayer = TAGManager.instance().dataLayer
dataLayer.push(["ecommerce": ""])
dataLayer.push(["ecommerce": NSNull()])
dataLayer.push(["ecommerce": ["promoView":["promotions": promotions!] ],"event":"ecommerce" ])

_ = AlertController.presentViewController(message!,
icon:UIImage(named:"alerta_hello.png"),
titleButton: NSLocalizedString("general.continue",comment:""),
action: {() in

let dataLayer: TAGDataLayer = TAGManager.instance().dataLayer
dataLayer.push(["ecommerce": ""])
dataLayer.push(["ecommerce": NSNull()])
dataLayer.push(["event": "promotionClick", "ecommerce": [ "promoClick" : ["promotions": promotions!] ] ] )

if let controller = UIApplication.shared.keyWindow?.rootViewController {
if controller.presentedViewController != nil {
if let tutController = controller.presentedViewController as? ImageDisplayCollectionViewController {
tutController.closeModal()
}
}
}
customBar.handleNotification(type!,name:name!,value:value!)
}
)
}
}
UIApplication.shared.applicationIconBadgeNumber = 0
UIApplication.shared.cancelAllLocalNotifications()
}

}

有谁知道我必须在我的代码中添加什么才能在 Apple Watch 上显示此通知?我已经添加了 Watch Os Target,但我不知道在该设备上的何处声明此远程通知。

最佳答案

根据 Apple Watch Notification Essentials :

Apple Watch displays notifications at appropriate times. When one of your app’s local or remote notification arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on Apple Watch.

因此,您无需添加额外代码即可在 Apple Watch 上显示您的通知(如果您想实现一些自定义通知操作,那就是另一个 story )。请记住,默认情况下只能在物理设备上测试推送通知,正如您评论的那样,这就是您无法在模拟器中收到通知的原因。

这是一个 library to test remote notifications on iOS Simulator (我找不到是否有人用 Apple Watch Simulator 测试过这个,你必须试一试)。

关于ios - 如何将 Iphone 的远程通知传递给 Watch OS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com