Chapter 6: Notifications (Page 105)

let imageNumber = content.userInfo[“imageNumber”] as? Int

does not work. imageNumber is always nil.

So, instead of

let validRange = 1…20
if
let imageNumber = content.userInfo[“imageNumber”] as? Int,
validRange ~= imageNumber {
image = Image(“cat(imageNumber)”)
} else {
let num = Int.random(in: validRange)
image = Image(“cat(num)”)
}

Following code

if let aps = content.userInfo[“aps”] as? [AnyHashable : Any],
let imageNumber = aps[“imageNumber”] as? Int, validRange ~= imageNumber {
image = Image(“cat(imageNumber)”)
} else {
let num = Int.random(in: 1…20)
image = Image(“cat(num)”)
}

works.

Hi @katsuhiro, welcome to forums. You’ve placed the imageNumber in the wrong spot. Remember that the “aps” dictionary is 100% owned by Apple, and you should only put the keys they define there. In your case, “imageNumber” is a custom key known about by your app. Look back in the chapter and you’ll see that it says to add the line right before the final } character. Essentially, “imageNumber” should be a top-level dictionary key, just like “aps”, “WatchKit Simulator Actions” and “customKey” are.

1 Like

hi,gargoyle. Thanks for your reply.

i was wrong.

“imageNumber” should be a top-level dictionary key.
like the following json.

{
“aps”: {
“alert”: {
“body”: “Tap me to see an adorable kitty cat.”,
“title”: “Giggle Time!”,
},
“category”: “myCategory”
},
“imageNumber”: 5
}