我正在开发一个非常庞大的应用程序,它已经有远程推送通知。这些通知还包括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,但我不知道在该设备上声明此远程通知的位置。
答案 0 :(得分:3)
根据Apple Watch Notification Essentials:
Apple Watch会在适当的时间显示通知。当其中之一 您应用的本地或远程通知到达用户的iPhone, iOS决定是在iPhone上还是在iPhone上显示该通知 Apple Watch。
因此,您无需添加额外的代码,以便在Apple Watch上显示您的通知(如果您想实施一些自定义通知操作,那就是另一个story)。记住推送通知默认情况下只能在物理设备上进行测试,如您所评论的那样,这就是您无法在模拟器中收到通知的原因。
继承人library to test remote notifications on iOS Simulator(如果有人用Apple Watch Simulator测试了这个,我就找不到了,你将不得不试一试)。