iosswiftapple-push-notificationsapple-watchremote-notifications

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


I'm developing a very huge App, that already has Remote Push Notifications. This notifications also includes the Facebook ones, and they're showing very well at the iPhone and iPad, the thing is that now I need to show the same notifications at the Apple Watch, and I have no idea how can I do that.

This is my AppDelegate, where I declare de remote notifications:

  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()
        }

    }

Does anyone knows what do I have to add to my code for showing this notifications on an Apple Watch? I already have added the Watch Os Target, but I don't know where to declare this remote notifications on that device.


Solution

  • According to 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.

    So, you don't need to add extra code in order to get your notifications displayed on the Apple Watch (If you would like to implement some custom notification action, thats another story). Remember push notifications can only be tested on physical devices by default, as you commented, that was the reason why you couldn't receive the notifications in the simulator.

    Heres a library to test remote notifications on iOS Simulator (I couldn't find if someone have tested this with the Apple Watch Simulator, you will have to give it a try).