简体   繁体   中英

IOS Notification implementation design technique

I am currently developing my application using Parse.com services to send pushes.

I have successfully implemented so that the pushes are being sent and received properly. What I need help with is how to think when actually trying to do something with the notifications.

I know the how to but I have some questions that are bothering me and it is pretty hard to search specifically for a question like this on the web so after hours of searching and thinking I give up and thought of sharing my question with you guys.

Now moving on to the actual question:

Where am I supposed to implement the code to handle the push notification to do something? In my application, there will be different push notifications for different purposes, each requiring the application to do something. Am I supposed to write the same code to handle push notifications on every viewController that I have(cause I am thinking that I cant really know where the user will be when the push is received) or obviously there is a much smarter/cleaner solution?

Lets say that a specific push notification should make an alert come up where a user can press on two buttons. If I had three different views meaning three different viewControllers would I need to implement the same code to do the alert on each VC?

If my question is unclear please let me know and I will try to explain more throughly. Thank you..

I usually create a class that handles notifications and has a reference to sth i call presentation manager which knows how to instantiate view controllers (it basically keeps a reference to a storyboard and handles windows setup for the app delegate). The app delegate instantiates this notification handler and forwards all local / remote notifications to it.

For presenting UI for notifications i chose to use an operation queue like the one apple described during the WWDC15 session "Advanced NSOperation". I just schedule operations that present a VC and complete when the VC is dismissed. Those UI-operations are mutually exclusive (see AlertOperation in apples sample code). That way i don't display multiple VCs when a lot of notifications are received but the user has not yet closed the UI for the first one.

We present such "modal" VCs by traversing to the innermost presentedViewController from the main UIWindow's rootViewController . That gives you a VC that you can call presentViewController() on as long as you don't use special presentation context configurations. (Again, a presentation manager that can do this lookup for you comes in handy here)

If you implement a custom presentation controller for that presentation, you can also create a custom VC and make it look like a modal dialog / popup. Or a bar that comes down from the top like Facebook Messenger uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM