Push Notifications · Tap to App | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1258151-push-notifications/lessons/5

Hey there, gread video and thanks for that!
But how can we add this action to open an ViewController after tapping the Notification while the App is running in background? That seems to be very difficult and I cannot find any working solution for that


Great Job.

thanks Thomas! it’s only for convenient testing that I show the push notification arriving with the app running. Normally, notifications arrive when the app isn’t running. I’ve just sent this notification to my phone in sleep mode (screen off) after quitting the app (dragging it out of the quick view) — tapping the notification opens the otter spot view controller in the app.

Hi audrey, thanks for your fast reply.

If I use the code like you, I get " Fatal error: Unexpectedly found nil while unwrapping an Optional value:" which is caused from the line where I force unwrapp the rootviewcontroller.
I do not know why this is happening, and in your code it works very well.

hi Thomas! Are you trying to open a view controller in your own app? Make sure you’ve given it a View Controller Title in its attributes inspector or in code, to match the withIdentifier argument.

You don’t get this error when the app is running in the foreground?

Hi @audrey
Downloaded the tap to app resource files - https://www.raywenderlich.com/1258151-push-notifications/lessons/5, customised bundle identifier and the app refuses to run.
the app crashes and I see this in debug info:
2020-01-27 18:12:55.261473+0530 Living[650:336934] [Storyboard] Unknown class _TtC12OtterSpotter31NotificationTableViewController in Interface Builder file.

Could not cast value of type ‘UITableViewController’ (0x1e6f2feb8) to ‘Living.NotificationTableViewController’ (0x100592b70).

2020-01-27 18:12:55.271904+0530 Living[650:336934] Could not cast value of type ‘UITableViewController’ (0x1e6f2feb8) to ‘Living.NotificationTableViewController’ (0x100592b70).
(com.something.Living is the bundle Identifier, with Living as Display Name.)

This issue is starting to get weirder.
I said in my last comment, after changing bundle identifier, the app starts crashing. Try changing the bundle identifier and then running app on iPhone, do this thrice (both changing bundle ID and then installing) and the app should crash.
Also,
another case in which application crashes. Under general settings, tick iPad as well under target devices, after installing in an iPad with it unchecked previously and now the app should definitely crash. The strange thing is, if I uncheck it again, the app still crashes at
let vc = nc.topViewController as! NotificationTableViewController
in AppDelegate

Please give me a solution because I believe I am doing something silly and wasting a lot of time unnecessarily.

hi Akashlal! It looks like you’ve customised the sample project more than the bundle ID. Does the storyboard entry point arrow still point to the navigation controller whose root vc is the table view controller? If not, modify those lines in AppDelegate to match what you have.

Also, I don’t understand these extra characters:

_TtC12OtterSpotter31NotificationTableViewController

Did you change the name of the project? I’ve never been able to get that to work properly.

I am very sure that I am changing nothing except changing display name and bundle id, and enabling iPad in target settings.

Let me record a video and post its link here. Give me a moment

Here is a link to video screen grab - https://cl.ly/37812a

See? Just changed bundle ID and the error came up.
2020-01-26 20:50:41.247543+0530 Living[4445:278411] [Storyboard] Unknown class _TtC12OtterSpotter31NotificationTableViewController in Interface Builder file.

Could not cast value of type ‘UITableViewController’ (0x7fff898b0d20) to ‘Living.NotificationTableViewController’ (0x1062c0c88).

2020-01-26 20:50:41.282767+0530 Living[4445:278411] Could not cast value of type ‘UITableViewController’ (0x7fff898b0d20) to ‘Living.NotificationTableViewController’ (0x1062c0c88).

OtterSpotter.zip (1.6 MB)

Also uploaded a copy of my file here for you to review, in case you are unable to see the same issue.

I’ve never tried to change the display name like that. I think you have to also change/add something in Info.plist. See:

Okay @audrey , tried iOS Creator’s tutorial,
took the project showing error
OtterSpotter.zip (1.6 MB)
Kept Display name as Living,
Bundle Identifier as com.akashlal.Living,
and finally, added a row in info.plist
with Bundle display name as key and Living as a string value.

The app still crashes.

Could you let me know what could be the issue? Because once that crash happens, it still crashes even after I change display name and Bundle Identifier back to what it was. Rendering everything I did till now useless.

Also, till date, I have worked on at least 30 apps, all I did to change display name was Change it in target settings, and didn’t need to add it in info.plist file. I guess that isn’t required now, its 2020 and Xcode should have figured out to do the necessary changes if I change display name which is even the exact name of the setting.

I think that article only adds the Info.plist item, and doesn’t change it on the project page. You should ask about this in an Xcode forum, perhaps at Apple.

Hi back Audrey,
I mean the concern is not about me not being able to change the display name of the application. The concern is, app crashes on changing display name.
If I delete line 52,53,54 of AppDelegate, the app runs fine.
Those lines look like something used to update data for silent notifications.

Also, what do those lines do? I don’t know Core Data, will learn it this weekend.

The code in AppDelegate is setting up the Core Data data store and setting the table view controller’s property.

When you change the display name on the project page, it changes the product name, which creates a mismatch between what Xcode is looking for — Living.NotificationTableViewController — and what’s in the project — OtterSpotter NotificationTableViewController. The app crashes.

I did it this way, and looked at the Info tab of the project page: now it has a Bundle display name item, set to product name.

If you add the Bundle Display Name item to Info.plist, it changes only the display name, and leaves the product name as it was. The app runs.

When I set the display name in Info.plist, the Info tab of the project page doesn’t show any bundle display name item, which is puzzling. I then changed the display name on the project page, and the item appeared in the Info tab, set to product name. Then the app crashes just like yours.

I changed the display name on the project page back to OtterSpotter, and the app runs.

I didn’t change the bundle ID, so maybe that’s what kept your app crashing.

1 Like

Okay, for now I will use only .plist Display name property to change app display name :slight_smile:
Thank you for troubleshooting!

Updating this thread after learning Core Data:
The crash was due to core data model which was linked to older project name/bundle ID

1 Like