When my iOS app exits, it registers a series of local notifications, which update the badge number at specific times. The local notifications do not bring up a popup, they simply update the badge. On my old iPod touch which does not support multitasking, this works perfectly. However, on my multitasking enabled devices, I am experiencing a very strange bug: when I have "exited" the app (i.e. it is still running in the background, but I am doing something else), the local notifications are not firing. Is there any reason why the local notifications would not fire when the app is in the background?
The code to create the local notifications runs in a loop (I create a bunch of them):
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.applicationIconBadgeNumber = totalCount; // a number generated earlier in the code
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = endDate; // a date generated earlier
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
And also I have created the following function in my app delegate, which tells me how many notifications are set up before the app enters the background:
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"# Notifications: %d", [[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
}
The app constantly tells me that there are 64 notifications (the number that should be set up) when it enters the background.
Check the following from Apple's developers docs:
"Each application on a device is limited to 64 scheduled local notifications. The system discards scheduled notifications in excess of this limit, keeping only the 64 notifications that will fire the soonest. Recurring notifications are treated as a single notification."
Could you problem be related to the number of notifications been scheduled?
You can find further information in http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html
Well #Jason you need to set the alertBody of the local notification atleast to show an alert view, thats all there is to it.
Also if you dont wont to show the view option in the alert box then set the hasAction attribute to NO.
Swift Version:
func applicationDidEnterBackground(application: UIApplication) {
let notification = UILocalNotification()
notification.alertBody = "App has been entered in background"
notification.alertAction = "open"
notification.fireDate = NSDate()
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Related
Any possibility to reset/clear app badge without any app interactions such as opening the app or any push/local notifications. My need is, I want to reset my app badge starting off every day without even opening the app or with any notifications.
You can schedule local notification for end of day with badge count equal 0. Check this out Updating iOS badge without push notifications
I solved this problem with the help of this solution. The working code is:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSCalendarUnit unitDay = NSCalendarUnitDay;
localNotification.repeatInterval = unitDay;
localNotification.fireDate = fireDate;
localNotification.applicationIconBadgeNumber = -1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Local notification has no alertBody, so local notification will not display upfront.
If need to show alertBody then add localNotification.alertBody = #"Your notification alertBody";.
Thanks for the answer.
I realizing push notification handling on my iOS application on Xamarin.iOS platform but my problem actual for iOS native too.
I need to handle push notification when my application is in Background Mode for some reasons, so I turn on Background Mode and Push Notification using in my project. Also I include the content-available key with a value of 1 into the payload’s aps dictionary. Also I include alert, badge and sound keys because I want to show this push notification for user and add it into Notification Center.
As a result after push notification receiving in Background Mode (when application is not active):
1) I handle push notification receiving using DidReceiveRemoteNotification() method.
2) The user see notification rolls down from the top of the screen as a banner.
3) I change application icon badge counter.
My problem is that push notification don't adds to Notification Center after all this actions.
As I understand after handling the push notification in DidReceiveRemoteNotification() method iOS mark this notification like handled and doesn't add them to Notification Center. As possible solution I can create Local Notification, schedule them and it'll added to Notification Center but the user will again see notification rolls down from the top of the screen as a banner and that's not good (it looks like user gets 2 notifications but only 1 shows in Notification Center).
What's the actual reason for this behaviour and how can I solve this problem?
You can try VoIP Push notification Service.
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
NSDictionary *payloadInfo = payload.dictionaryPayload;
[self showLocalNotificationWithInfo: payloadInfo];
}
- (void) showLocalNotificationWithInfo:(NSDictionary *)infoDict{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = #"Your title message";
localNotification.soundName = #"YourSoundFileName.mp3";
localNotification.userInfo = userInfo;
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Apple VoIP Push Service
Thank you guys for your answers. I found a reason of this behavior - the problem was with badge key in push notification body. This key had 0 value and thats why iOS marked new notifications as readed and they didn't add to Notification Center.
Is it possible to open an app from a background task in iOS?
I want my app to run in the background, and I want it to reappear for whatever reason without user input. Is this possible?
In Android you can have a service that runs in the background, and at any time you can start an Activity, which starts the app for that activity.
I know this does not make for a good user experience. I still want to know whether it can be done.
You can create schedule a local notification with a time offset of 0 (eg show an alert immediatly). If the use taps on the notification, the app will be launched.
But otherwise, you will not be able to launch out of the background into the forground without a notification.
NSDate * theDate = [[NSDate date] dateByAddingTimeInterval:0]; //
UIApplication* app = [UIApplication sharedApplication];
NSArray * oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];
// Create a new notification.
UILocalNotification* alarm = [[UILocalNotification alloc] init];
if (alarm)
{
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = #"sonar";
alarm.alertBody = #"Background task complete, tap to show app" ;
[app scheduleLocalNotification:alarm];
}
From Apples Documentation:
"Notifications are a way for an app that is suspended, is in the background, or is not running to get the user’s attention. Apps can use local notifications to display alerts, play sounds, badge the app’s icon, or a combination of the three. For example, an alarm clock app might use local notifications to play an alarm sound and display an alert to disable the alarm. When a notification is delivered to the user, the user must decide if the information warrants bringing the app back to the foreground. (If the app is already running in the foreground, local notifications are delivered quietly to the app and not to the user.)"
I have the following code implemented in applicationDidEnterBackground:
-(void)applicationDidEnterBackground:(UIApplication *)application
{
// Fire date is set to 30 secs and repeat interval is set to 1 min
// for testing purpose.
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Test Notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSMinuteCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
The variable localNotification is initialised in application:didFinishLaunchingWithOptions.
All local notifications are cancelled in applicationDidBecomeActive.
Steps:
1) The app Notification Center is initially On when the app is put into background by the user hitting the HOME button.
2) Notification shows up accordingly.
3) Turn app Notification Center to Off in Settings.
4) Notification still shows up.
Notification will be not shown if the app Notification Center is turned off. Is this correct? If my assumption is wrong, how can I cancel the notification when the user turns off the app Notification Center. Deployment target is iOS5.1. iPhone is running iOS6.1.
I’m not sure I understand you, but when you pull an app out of notification center it stops showing it in the notification center window, but it’ll still show the banner or alert on the screen unless you turn that off as well.
I have noticed that when a local notification is being received in an ios device, the notification appears in the Notification Center but the app badge number is not updated when the app is closed.
I need to touch the notification in the Notification Center for the local push message to be transferred to the app.
Is this the normal behavior? Can this be solved by using remote push notifications?
You can utilize the applicationIconBadgeNumber parameter in a UILocalNotification object.
Basically:
localNotificationObject.applicationIconBadgeNumber++;
Example:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20];
localNotification.alertBody = #"Some Alert";
//the following line is important to set badge number
localNotification.applicationIconBadgeNumber++;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
But the issue with this is that the badge number doesn't increment on subsequent (multiple) local notifications (there's a scenario here but for simplicity sake, lets just say the badge stays 1 even after 2 or more, back to back, local notifications).
In this case, Yes... Push Notification seems to be the way to go
(but be aware that Push Notifications aren't always reliable... check: link)
Well... to use Push Notifications for proper badge number updates, you should know that you can send a badge count in the Push Notification's payload.
When this push notification is received, the badge count is changed by iOS to the badge count specified in the Push Notification (& the app need not be open for this).
Example (continued):
Set applicationIconBadgeNumber to 0 as it helps in certain scenarios (optional)
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Extra:
You can also manually set the badge number when you terminate/close or resign the application.
Generally... in any or all of the following methods:
-applicationWillResignActive
-applicationDidEnterBackground
-applicationWillTerminate (set badgeNumber when app closes)
Example:
- (void)applicationWillResignActive:(UIApplication *)application {
//Called when the application is about to move from active to inactive state.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
//...
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
//...
}
iPhone: Incrementing the application badge through a local notification
It is not possible to update dynamically the badge number with local notifications while your app is in the background. so You have to use push notifications. You can only increment badge while application is running in foreground and look for alternative solution you can go with here
iPhone: Incrementing the application badge through a local notification