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

How to pass the remote notifications I have for Iphone to Watch OS?

我正在开发一个非常庞大的应用程序,它已经具有远程推送通知。此通知还包括 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 目标,但我不知道在该设备上的何处声明此远程通知。

根据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 测试过这个,你必须试一试)。