Reputation: 2205
I am successfully able to receive push notification on my device which is of type alert.
1. Active State OR App is in foreground
didReceiveRemoteNotification method is getting called of the app. Here I can access received notification message.
2. In-Active State OR App is in background
Message of the Push notification is displayed in alert box with "View" & "Close" button.
a. On click of "view" button didReceiveRemoteNotification method is getting called. Again I can access received notification message.
b. On click of "Close" button, message will be disappeared. In this case I do not have any handler regarding the last received push notification.
3. App is not in background OR App is not running
Same as case 2.
4. iPhone is in sleep mode
Alert is displayed with message but no "View" or "Close" button.
a. On UI it will display "Slide to View". If user interacts with phone it will open the app. I can access received notification message.
b. If user do not interacts and allow phone to go in sleep mode or manually put phone in sleep mode then for the next time iOS displays "Slide to unlock" message. Here again I don't have any handler for the last push notification.
How can I access the last push notification of case 2.b and 4.b, Is there any method which best called which I am missing to implement.
EDIT 1: Below is my code of appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[Test1ViewController alloc] initWithNibName:@"Test1ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// For PUSH NOTIFICATION
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
}
}
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(@"Received notification: %@", userInfo);
// Received Notification at here ....
// Now based on response further to do ...
}
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
Thanks, J61
Upvotes: 1
Views: 3708
Reputation: 4291
The OS will take care of badges. But if the user dismisses your push notification, your app could take a look at messages on your server and handle it the appropriate way if it has missed any notifications the user dismissed.
Taken from here, this is the way you can handle the notifications:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// get state
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
// App is in foreground
} else {
// App is suspended
}
application.applicationIconBadgeNumber = 0;
}
Edit: If you want to auto-increment your badge number, you need to keep track of the count for yourself. You could do this on your server. If your push message doesn't contain a badge count, iOS will remove it from your application.
Upvotes: 3