I have my iPhone application implemented with a push notification. I successfully implemented the push notification basically. When I tapped the push notification message, the message will display on the message label on the ViewController. However, when I open the icon(Application), it didn't return any notification. I need to display the push notification not only in tapping the push notification but It should work if the iOS user just opens the app too.
here is my code.
AppDelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo{
NSString *messageAlert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
NSLog(@"Received Push Message: %@", messageAlert );
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:messageAlert];
on my ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:@"MyNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
NSString *_string = note.object;
messages.text = _string; //message
}];
}
}
It will display the notification though when the notification i clicked. But when i open the app, the notification must display the message too. How to do that? please help me.
There are mainly four states of push notification comes into the device. Let us consider one by one each case :-
CASE 1: when the application is truly not loaded in memory (e,g. when you launch it the splash screen shows up etc), then application:didFinishLaunchingWithOptions is called, and you can get the push notification as follows:
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif)
{
//Handle remote notification
}
CASE 2: if the app is loaded in memory and is ACTIVE (e.g. the app is currently open on the device) then only application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo is called. Normal one, as you told succeeded in this case.
CASE 3: if the app is loaded in memory but is not ACTIVE and NOT BACKGROUNDING (e.g., you launched the app, then pressed the home button, and waited 10 seconds), and then you click the action button on a push notification, only didReceiveRemoteNotification is called. follow below approach.
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if([app applicationState] == UIApplicationStateInactive)
{
//If the application state was inactive, this means the user pressed an action button
// from a notification.
//Handle notification
}
}
CASE 4 : when application is in background and then tap on icon , then according to Apple 's documentation you can't retrieve all the notification which are pending.
Note: for solving this problem Apple introduces the concept of background launching of application in iOS 7. But till now you can't do that means till iOS 6 development you have to stuck on it.
Hope this Helps you !
When user tap on app icon, the method didReceiveRemoteNotification does not fire. You need to catch the notification in application:didfinishLauncingWithOptions. Some thing like this:
NSDictionary *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(@"launchOptions: %@", launchOptions);
NSLog(@"localNotif: %@", localNotif);
if (localNotif) {
NSDictionary *itemName = [localNotif objectForKey:@"aps"];
NSLog(@"dict: %@, aps: %@", localNotif, itemName);
//your methods to process notification
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With