jdefSF
jdefSF

Reputation: 41

WatchOS dynamic notifications not working on Xcode 11.1 and later - didReceive not called

I'm trying to get the Apple Watch SwiftUI notification tutorial working from https://developer.apple.com/tutorials/swiftui/creating-a-watchos-app. It works on Xcode 11.0, but fails on any more recent releases all the way from 11.1 through beta 11.3.

didReceive in the NotificationController is not being fired when a notification is received. Here's the code for the NotificationController.swift file:

import WatchKit
import SwiftUI
import UserNotifications

class NotificationController: WKUserNotificationHostingController<NotificationView> {
    var landmark: Landmark?
    var title: String?
    var message: String?

    let landmarkIndexKey = "landmarkIndex"

    override var body: NotificationView {
        NotificationView(title: title,
            message: message,
            landmark: landmark)
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    override func didReceive(_ notification: UNNotification) {
        let userData = UserData()

        let notificationData =
            notification.request.content.userInfo as? [String: Any]

        let aps = notificationData?["aps"] as? [String: Any]
        let alert = aps?["alert"] as? [String: Any]

        title = alert?["title"] as? String
        message = alert?["body"] as? String

        if let index = notificationData?[landmarkIndexKey] as? Int {
            landmark = userData.landmarks[index]
        }
    }
}

Also, here's the content of PushNotificationPayload.apns:

{
    "aps": {
        "alert": {
            "title": "Silver Salmon Creek",
            "body": "You are within 5 miles of Silver Salmon Creek."
        },
        "category": "LandmarkNear",
        "thread-id": "5280"
    },

    "landmarkIndex": 1
}

Is anyone else having this issue?

Upvotes: 4

Views: 1196

Answers (2)

GilroyKilroy
GilroyKilroy

Reputation: 894

I was having the same exact problem with Xcode 11.3. It failed in my code but not in Apple's completed project. Looking carefully at the tutorial code the payload there is (partially):

    "alert": {
        "body": "You are within 5 miles of Silver Salmon Creek."
        "title": "Silver Salmon Creek",
    },

and the payload in the completed project is:

    "alert": {
        "title": "Silver Salmon Creek",
        "body": "You are within 5 miles of Silver Salmon Creek."
    },

They reversed the two lines. However looking closely you see in their tutorial code the "body" line is missing the comma at the end. This causes the JSON parsing to fail (silently) and not generate the alert. Stick the comma in and it works fine.

Upvotes: 1

jdefSF
jdefSF

Reputation: 41

Dynamic Notifications are now working in WatchOS beta 6.1.1

Upvotes: 0

Related Questions