I need a way to segue way to a view controller when the app opens from a NSLocalNotification.
I don't really know where to start and I can't find any examples on how to do it online. Any suggestions are appreciated.
Where to start:
Do you know how to initiate a segue through code? If not, see the UIViewController docs and search around here for prepareForSegue and performSegueWithIdentifier There are lots of hits. Also this is a good tutorial: iOS 5 Storyboard: How To use Segues, Scenes and Static Content UITableViews but there are many out there.
Have you worked with UILocalNotifications? See the UILocalNotification Programming Guide for an overview.
In terms of handling the event, that is done by the UIApplicationDelegate See the event method: application:didReceiveLocalNotification:
Local notifications are similar to remote push notifications, but
differ in that they are scheduled, displayed, and received entirely on
the same device. An application can create and schedule a local
notification, and the operating system then delivers it at the
schedule date and time. If it delivers it when the application is not
active in the foreground, it displays an alert, badges the application
icon, or plays a sound—whatever is specified in the
UILocalNotification object. If the application is running in the
foreground, there is no alert, badging, or sound; instead, the
application:didReceiveLocalNotification: method is called if the
delegate implements it.
How you handle a UILocalNotification will depend on the state of your app: foreground, background, inactive.
Related
Question:
Is there a way to detect if you get a system notification and you swipe away the notification banner (e.g. the alarm that triggers a system notification)?
More practical example:
In your app, you are doing some stuff and suddenly you get a notification from another app. Instead of tapping on the notification, you swipe up the notification (you dismiss it that way). After doing that, I want to capture that in the app.
Why:
Currently, I have a bug in the app, so if I can capture this action, I would be able to fix this bug.
Thanks in advance!
When notification appears, the willResignActiveNotification won't get called, therefore willEnterForegroundNotification won't get called, too.
The best solution here is to catch incoming notification and show the custom view describing this notification instead of system one. Thus, you can detect all actions/timings you need.
When I open the app it fires the events viewDidLoad and viewDidAppear form my View Controller but when I close it and run it again it does not call any of them.
Any idea?
You need to read up on application states. Here is a link I found online outlining the different states:
http://www.techrepublic.com/blog/software-engineer/understand-the-states-and-transitions-of-an-ios-app/
What you really want is to be notified when your app becomes active.
Probably the easiest way is to implement the function applicationDidBecomeActive() in your app delegate. That will be called when your app becomes active as the foreground app either on launch, or when it returns to the foreground as the active app.
Note that if you want that notification sent to some object other than the app delegate you can listen for the UIApplicationDidBecomeActive notification.
I am making an app with multiple schedule alarms in the future. The design is that a Local Notification will fire, then when the user opens the app any time after this notification (either through Notification Action or just by tapping the icon on the home screen), the app will automatically show an Alarm view controller, which the user interacts with.
I know I can just call on a function with performSegueWithIdentifier with the Notification Action feature, but I would really like a comprehensive method that covers the case when the user ignores the notification, then launches the app with other methods.
Thank you for your help!
App is not running. Interactive push notification comes in. User taps a button on it. App is launched, notification action is handled, but app state is still inactive. Even when I background/foreground the app, it's still inactive. If I follow the notification directly (tapping the notification, not the interactive button) from the same notification, the app gets launched and is in active state. Why doesn't it always go to active state?
Have some thoughts on this.
First of all, it's not a given that a notification will result in a user opening up the app.
Second, there are notification callbacks in the delegate. Is it possible to manually set the application state?
If not, and I don't want to jump to conclusions, but this sounds like it may be a bug, and you may want to file a radar.
Although, you may be able to work around it. Look at those delegate methods.
According to didReceiveRemoteNotification when in background , we used to be able to handle the user opening the app by clicking the action button on a push notification (or swiping on the push notification, depending on how the user sees push notifications) by implementing -application:didReceiveRemoteNotification: and then checking inside the method whether the application's applicationState was not active.
In iOS 7, there's the new remote-notification background mode, which allows the app to perform background fetch when a remote notification is displayed to the user (without the user necessarily doing anything to the notification). To support this mode, you are supposed to implement the -application:didReceiveRemoteNotification:fetchCompletionHandler: method.
The documentation for -application:didReceiveRemoteNotification: says that if your application delegate implements the application:didReceiveRemoteNotification:fetchCompletionHandler: method, then "the app object calls that method instead of this one." Which means we cannot use -application:didReceiveRemoteNotification: to handle remote notifications anymore, since it's not going to be called.
We should probably put handling logic in application:didReceiveRemoteNotification:fetchCompletionHandler:, but the previous trick for handling it doesn't make sense anymore -- previously, we depended on the fact that the only way for -application:didReceiveRemoteNotification: to be called when the app is not active was if the user tapped the action button on the notification to open the app. However, now, the whole point of the remote-notification background mode is that it can call application:didReceiveRemoteNotification:fetchCompletionHandler: in the background every time a remote notification is received, before the user does anything to it.
So then, how can we now tell when the user opens the app using the action button on the notification?
You still check the application state in application:didReceiveRemoteNotification:fetchCompletionHandler:
UIApplicationStateBackground - App is in the background receiving a push notification
UIApplicationStateInactive - App is opening from the user tapping a notification
I was use this delegate function to add the 『Notification Number』.
Cause our Server not send the Badge to our Clients.
Then I used the strange method to add the 『Notification Number』 with this delegate function, and I also add a code to switch UIViewController in this function.
I found out when I use the server push Notification to my test App, and the status of test App is in the background, even I am using Twitter or Safari.
My test App also switch UIViewController to another UIViewController after I push Notification from the server.