Are push notifications stored somewhere on iphone? - iphone

If yes, how it's possible for the user to re-read them again ?
By coding, how do you read them to display them in your app ?

I'm pretty sure that the question was more about the notifications/banner messages which show up in the lock screen and all disappear when you unlock the phone, whether you actually read them or not. The phone just assumes you got them, and there is no way to retrieve them. It's one of the more poorly thought out aspects of the iPhone....up there with sorting contacts by last name if you enter one.

Push notification is not stored into application memory. But you can handle them as you want. There are mainly two case in which we need to handle the push notification:
1) Application is closed/background mode
2) Application is active mode
1) To Handle push when app is not running or in background, you need to click on push notification pop and it will open the application. Below is the code to handle this case:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
self.isNoti = TRUE;
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
// Do you stuff
}
return YES;
}
2) If your application is in active state:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
#try {
NSLog(#"Push Notification!!!");
}

Please, read Apple's documentation regarding push and local notifications to understand what's the difference and how they are different.
Also, check this tutorial which will guide you on developing a sample app. that supports push notifications.
I hope you find them useful

You can see saved banner notifications by swiping the screen from top to bottom if using IOS 5 on your iPhone. Hope this helps.

Related

Dealing with the remote notification

I have four screen in my application namely A,B,C,D .A is my first screen of the application appeared when application launch.Suppose C and D screens showing some data which are subscribed for the remote notifications in two different channels .with same client key and application id.
When remote notification come it is showing first screen i.e.A when
I tapped on notification.I want to show screen C or D as per type of the notification.
What is the con taint of the NSDictionary userInfo in the method
application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
When you would like to see the contents of a directory, just use NSLog. In this case, add the following code to the -application:didReceiveRemoteNotification: method.
NSLog("UserInfo: %#", userInfo);
That will print the contents of the directory to the log. Sorry I didn't give a more specific answer, the question was somewhat vague. Best of luck!
To show the screen C or D, check for application state in didReceiveRemoteNotification method.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateInactive) {
// check for condition for screen C or D and according to your condition redirect user to that screen.
}
}

Push notification data retrieving

I want to get data from push notification message. I successfully get the data when app is on foreground and in background. but I am unable to get data when app is quit and user press view button on push notification. I write the code in application did finish launching. This code cause the app crash when pressing on View button of push notification message. If I comment the code then app doesn't crash. Kindly help me to fetch data from push notification when app is quit and user press view button on push notification. I'll really appreciate that.
if(launchOptions != nil){
NSDictionary *tmpDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (tmpDic!=nil) {
pushedMessage=[NSString stringWithFormat:#"%#",[[tmpDic objectForKey:#"aps"] objectForKey:#"alert"]];
pushedCountry=[NSString stringWithFormat:#"%#",[tmpDic objectForKey:#"country"]];
[self saveToDatabase];
}
}
Please try this...
Add this code to appdelegate.m => didFinishLaunchingWithOptions
if ([launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"]) {
[self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}
From ios7 we have the below delegate method to handle the push notification when the app is in back ground or not running
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
When you click view button
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
{
}
this method is called and userinfo will contain all data
Things which you are doing in didfinishlaunch method do in didReceiveRemoteNotification:
I don't totally understand what your asking but you can do stuff with what is being being push with a function in the app delegate
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
{
NSLog(#"Received notification: %#", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
}
Now you can add that data to core data or sqlite. This may not be relevant to your question but it's the best I can give based on what you've asked in your question.
See my comment on the above answer.
Here is Apple's doc:
If the app is not running when a push notification arrives, the method
launches the app and provides the appropriate information in the
launch options dictionary. The app does not call this method to handle
that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
So you need to implement:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:
(NSDictionary*)userInfo
As well as handling the launchOptions in:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

iOS 5 banner push notification disappear, user tap on app icon to start the app

With iOS 5, push notifications can appear as banner and disappear after a few seconds.
I understand that didReceiveRemoteNotification will be called when user taps on the banner.
My question is, if the banner has disappeared and my user sees that there is a badge number on the app, they will tap on the app icon to start the app. Now if the app is running in the background, how do I check that the app is brought to foreground and there has been a notification, and do the necessary?
The purpose of my notification is basically to inform user there has been an update to the app content and encourage them to run the app to get the latest contents. My app only checks for latest contents at launch time and doesn't check for updates periodically.
This question is a bit old, but I'll pop what I've found in here anyway.
There are two methods you need to implement in your app delegate to check if your app was either launched from the remote notification (From when the app is not running on your device), or received the remote notification while running (in the background or foreground).
First is a method that is already in your App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
To check if this was launched from a remote notification, have some code similar to this:
// Check to see if launched from notification
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(#"Launched from push notification: %#", dictionary);
// DO SOMETHING HERE
}
}
The other method you will need to implement is specifically for the case that your application for when your application is running:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"Received notification: %#", userInfo);
}
How you handle the notification from there is up to you, but that's how your app knows about it!
In this second method, you can check the UIApplicationState of the passed application to find out if you were in the foreground or background.

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.

How do I tell if my iPhone app is running when a Push Notification is received?

I am sending Push Notifications to my iPhone app, and I'd like a different set of instructions to execute depending on whether the app is already launched or not. I'm new to iPhone development, and while I suspect UIApplication or my project's AppDelegate class has the solution, I haven't found a good answer. Is there an easy way to check for this?
Here's the more appropriate way of handling active/inactive state of the app.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// check for the app state
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
//the app is in the foreground, so here you do your stuff since the OS does not do it for you
//navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)
}
application.applicationIconBadgeNumber = 0;
}
didReceiveRemoteNotification: is called when the app is running, yes, but when it is suspended, the iOS takes care of putting up the badge, etc. If the app is in the foreground, the OS does nothing, and just calls your didReceiveRemoteNotification:.
Depending upon what you mean by "launched", you are either looking for:
Kevin's answer above (differentiates between launched or not launched)
or this (differentiates between suspended or active, but already launched):
Use a flag that is set true when the application becomes active, and false when the application is not active.
Flag (in header file [.h]):
BOOL applicationIsActive;
Code (in implementation file [.m]):
- (void)applicationDidBecomeActive:(UIApplication *)application {
applicationIsActive = YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
applicationIsActive = NO;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if (applicationIsActive) {
// Handle notification in app active state here
}
else {
// Handle notification in app suspended state here
}
This works because when the application is suspended, the OS calls "applicationDidReceiveRemoteNotification" before it calls "applicationDidBecomeActive" during the "wake-up" process.
The "complete" answer is actually Kevin's answer plus this answer.
Hope this helps.
The UIApplication delegate has the method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
which you need to implement. This receives the notification when the app is running.
If your app is not currently running and a notification is received then your app can be launched with
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
with the notification details held in the launchOptions dictionary. if the dictionary is nil then the user tapped the application icon as normal.
If you are going to check applicationState on iOS less than 4, you'll need to check that applicationState is supported:
if ([application respondsToSelector:#selector(applicationState)] ){
// Safe to check applicationState
UIApplicationState state = [application applicationState];
}
The Apple documentation for push notifications explains this:
However, there are two situations where applicationDidFinishLaunching: is not a suitable implementation site:
The application is running when the notification arrives.
The notification payload contains custom data that the application can use.
In the first case, where the application is running when iPhone OS receives a remote notification, you should implement the application:didReceiveRemoteNotification: method of UIApplicationDelegate if you want to download the data immediately. After downloading, be sure to remove the badge from the application icon. (If your application frequently checks with its provider for new data, implementing this method might not be necessary.)
This means that if your application:didReceiveRemoteNotification: delegate method is called, your app is running.