Can your iPhone app be notified of new notifications? - iphone

If your iPhone app is running is there a way to let the app know when a new notification comes in?
Normally the notification window pops up on the top then goes away, but that is handled by the system. Is there a call made to the app that is currently running?
While my app is running I would like to keep track of new notifications.
Thanks, Nick

There is a call made to the AppDelegate
-didReceiveRemoteNotification
-(void)application: (UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(#"%#",userInfo);
}

Related

How does iOS handle local Notification in background mode?

didReceiveLocalNotification is called when a notification is fired in active mode but how does iOS handles notification in background mode (not active mode, application is terminated may be) before app is active by swiping or clicking the notification.
Or
Mainly I want to know how to conditionally handle local notification to be on/off (off means not to cancel previous notification but just don't fire it) in background Mode?
I am currently checking this condition in didReceiveLocalNotification but that way I am not able to handle it in background mode?
You can check that is your app started by clicking on local notification in didFinishLaunchingWithOptions method in AppDelegate.m file.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
// your code
}
}
If application is running Alerts won't be shown automatically - you have to handle that manually using the functions you mentioned.
When application is in background or not running iOS will show your local notifications in notifications center and present them according to user settings - as a banner or alert.
Best read this:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html
When your app is in the background iOS will handle the notification (Local or Push) and your app can not access them at all.
Only after your app is started will you be able to direct if the user select a notification.

iOS Push Notification - How to get the notification data when you click on the app icon instead of notification

Similar to this question: How do I access remote push notification data on applicationDidBecomeActive?
But the different is how can you access the notification data when you are inapplicationDidBecomeActive and if you have clicked on the app icon instead of the push notification.
The flow is: If you click on the push notification then didReceiveRemoteNotification will be triggered, but if you click on the original app icon, only applicationDidBecomeActive will be triggered and didReceiveRemoteNotification will not be called.
I am looking for the later case so how can I access the push notification data.
(Both case assuming the app is in background and not killed yet.)
You can't get remote push payload by launching app from homescreen.
If the push data is important for app use, load it from your server after app launched.
#fannheyward answer is absolutely correct. You cannot get payload when application is launched by tapping app icon.
I have an idea, what if you get to know that some notification is pending when app is launched by tapping app icon. With this knowledge your app can fetch payload from your server.
You can set "Badge" in every such notification and on applicationDidBecomeActive you can check [application applicationIconBadgeNumber] > 0 to know that some notification is active. After fetching payload from your server you can set it to 0 like below
[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Please note: This means your app will have badge displayed over it when notification is received. I am not sure of the behaviour when badge is disabled by user from settings.
If your application target is over iOS7, you can do only if application is alive in backgroud.
In the capabilities settings at Xcode, you should enable Background Modes>Remote notifications, and write below code.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// save userInfo in NSUserDefaults
completionHandler( UIBackgroundFetchResultNoData );
}
If you want to test it, it will be helpful to use https://github.com/acoomans/SimulatorRemoteNotifications
From the server side, make sure to set content-available property with a value of 1
For this to work I also had to check the background fetch box.
You should get the notification in the launchWithOptions method in your appDelegate something like this:
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif != nil){
//Handle your notification
}

What code is required to launch an iPhone app after a local notification has been scheduled and displayed?

I am scheduling a local notification which is firing fine. I need to know how to allow a user to launch my app after the notification has been displayed on screen.
I also need to know how to identify this type of launch to direct the user to a specific view, relevant only to users who have arrived as a result of the notification.
I have been looking at the UIApplicationDelegate protocol reference and feel it could be in the area of the launchOptions within -didFinishLaunchingWithOptions:, but need a little pointer.
Your application will launch automatically, you don't have to do anything in the app itself for that to happen.
If that's not already happening, check that you've specified an alertAction for the notification when you create it - that's the label of the button in the notification alert that opens your app and if you don't set it, the notification alert won't launch the app.
To open a specific view, use the launchOptions and the application:didReceiveRemoteNotification method.
didFinishLaunchingWithOptions: would do the trick.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
// .. etc
};
Keep in mind you also need to implement
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
if you receive a push notification while the app is running (fore- or background)
The launch options should contain the key UIApplicationLaunchOptionsLocalNotificationKey which in turn gives you the UILocalNotification associated with the notification. Additionally, when scheduling the notification your UILocalNotification has the properties alertBody, alertAction and optionally alertLaunchImage which control the information being displayed. The action decribes the button text that launches your app while the body is the information being deisplayed above the buttons. You can supply a specific launch image for this launch to mimic the app already being run.
Also note Bogatyr's answer concerning the cases where your app is not being launched but already there, just suspended.

Can i get info about notification (apn)

Can I get list of push notifications (APNS notifications) for my app when app became from background to foreground mode?
In foreground mode i can receive info about push notification in callback
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Other case:
My app receive push notification, when app in background mode.
After this i click on app icon, and i want to get info about received notification. How can i get this info?
If i click directly on the notification (not on app icon), in background mode, then callback didReceiveRemoteNotification is call.
Can I get list of push notifications (APNS notifications) for my app when app became from background to foreground mode?
No. There is no list. You can only get one notification at once. When the users iPhone is offline and you send 5 notifications the user will only get the last one you have send.
If the user starts your app using the open action on the notification you will get it on start using:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Other Case
My app receive push notification, when app in background mode. After this i click on app icon, and i want to get info about received notification. How can i get this info?
You can't. When the user closes the notification and opens you app later it is already gone and there is no way to access it.
When you send push notifications you probably have a server reachable over the internet, where you register the devices of the user.
The usual way to handle this is to store the notifications on this server and query it on app launch... so use the notification just to notify the user to start your app and then check your server on launch of the app for whatever you want.
Once your application reaches foreground and actively running, the notification alerts like sound, alert won't be displayed or you wont be notified.
But you will get a call back in the UIApplication Delegate that you can make use of it.
The api is,
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
And, if your application is running in background, then the notification appears and only when you click on "View" button, you will get the call back in UIApplication delegate.
If you click on Close button, you won't get the call back in the application.

Push Notification Problem

I want to get the reminder at each notification comes from the web service in the application for the different tasks. For that what i have used the following methods.
but i am not getting push notific
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
Receiving push messages in the app
So what happens when your iPhone receives a push notification? There are three possible situations:
The app is currently running in the foreground. Nothing happens on the screen and no sound is played, but your app delegate gets a notification. It is up to the app to do something in response to the notification.
The app is closed, either the iPhone’s home screen is active or some other app is running. An alert view pops up with the message text and a sound is played. The user can press Close to dismiss the notification or View to open your app. If the user presses Close, then your app will never be told about this notification.
The iPhone is locked. Now also an alert view pops up and a sound is played but the alert does not have Close/View buttons. Instead, unlocking the phone opens the app.
I ran into a lot of problems on Push notification. Please note: if you are not getting the device token back, check to make sure the device is connected to the internet. If not device cannot contact the APN server. It took me days to find this out