Reputation: 27
I'm developing a chat application for iOS
5. I had a problem with local notifications.
When application went to background state, I am using this code:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertAction = @"Ok";
localNotification.alertBody = [NSString stringWithFormat:@"From: %@\n%@",message.sender,message.message];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
but when application is in active state ,and it is not in chat page then also I need the local notification but I used the same code there also the notification is coming in tray but banners are not coming....
Please help me out...
Upvotes: 0
Views: 1925
Reputation: 818
THe following code handles both remote and local notification
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[self showNotificationAlert:notification.alertBody];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *alertMsg = nil;
id alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
if ([alert isKindOfClass:NSString.class])
alertMsg = alert;
else if ([alert isKindOfClass:NSDictionary.class])
alertMsg = [alert objectForKey:@"body"];
[self showNotificationAlert:alertMsg];
}
- (void)showNotificationAlert:(NSString *)alertMsg
{
if (!alertMsg)
return;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Message", nil)
message:alertMsg
delegate:self cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil];
[alert show];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
Upvotes: 0
Reputation: 16864
Following method put into your application Delegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UILocalNotification *localNotif =notification;
NSString *strBody=[localNotif.userInfo valueForKey:@"Body"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Application name"
message:strBody
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
//NSLog(@"Incoming notification in running app");
// Access the payload content
//NSLog(@"Notification payload: %@", [notification.userInfo objectForKey:@"body"]);
application.applicationIconBadgeNumber = 0;
}
Upvotes: 1