I've been using a UILocalNotification in my iPad App & it worked for some time until recently.
I can't produce any notifications at all, no matter what I try. Here's my last attempt:
-(void)applicationDidBecomeActive:(UIApplication *)application{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *date = [NSDate date];
NSDate *dateToFire = [date dateByAddingTimeInterval:300]; // 5 minutes
[localNotification setFireDate:dateToFire];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Incoming Local Notification" ];
[localNotification setAlertAction:#"Open App"];
[localNotification setSoundName:UILocalNotificationDefaultSoundName];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
My iPad is set to Auto-Lock=2minutes.
Shouldn't there be a notification after 5 minutes considering the code above?
Yes that should work. Keep in mind that local notification (as well as push notification) will not be shown if your application is currently open.
If your app is open you need to look for call of
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
in your app delegate.
It seems that when updating from iOS4 to 5 Apple disables notifications for Apps that already have been installed under 4. If you go to Settings -> Notifications you can reenable them. That solves the problem.
Related
I want to set a notification that repeats every 14 days(2 week).I am using old notification framework "UILOCAL NOTIFICATION" as i want it to work with only ios 9.
Either you must do as this answer says: Repeating a Local Notification after every 14 days(two weeks)? or you must let your app handle this by scheduling a local notification for the first occurrence, and then when this notification is received by your app, reschedule it for another 14 days.
Yes you can do easly,
Epic: When the user leave the app, we set UINotification and then send it. if the user not enter in 2 days(2*24*60*60), the notification will send end of the 2 days. if you want to send after 14 days, you can change the fireDate time with 14*24*60*60
in AppDelegate :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2*24*60*60];
//[[NSDate date] dateByAddingTimeInterval:5];[NSDate dateWithTimeIntervalSinceNow:24*60*60]
NSLog(#"%#",notification.fireDate);
notification.alertBody = NSLocalizedString(#"NOTIFICATION_MSG", #"Message");
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
application.applicationIconBadgeNumber = 0;
}
I've currently got local notifications set so that users can select a time on the datePicker (hour, minute, and am/pm) when they would like notifications sent. It works fine... up until the next day. It seems that the notification only works on the same day, and resets once midnight hits. How do I prevent the alarm from resetting, so that a notification is sent daily?
Here's my code so far:
(IBAction)scheduleNotification:(id)sender {
UILocalNotification *notification = [[UILocalNotification alloc]init];
NSDate *fireDate = _datePicker.date;
[notification setFireDate:fireDate];
[notification setAlertBody:#"Daily Reminder"];
[notification setAlertAction:#"Go to app"];
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
//Local push notifications
}
(IBAction)didChangeDatePicker:(id)sender {
NSLog(#"New reminder time selected: %#",self.datePicker.date);
}
You need to add:
[notification setRepeatInterval:NSDayCalendarUnit];
It will then recur every day until you cancel it with:
[[UIApplication sharedApplication] cancelLocalNotification:notification];
or
[[UIApplication sharedApplication] cancelAllLocalNotifications];
which by the way only cancels notifications set by your app.
Too, this is well-presented in the documentation if you search on UILocalNotification.
in my app i schedule some UILocalNotification every time the app is opened, but happen that sometimes the uilocalnotification is not fired and don't notify me,and when happen they don't fired anymore, to fix it i have to reinstall the app on the iPhone, so i'm explain me well, there is a period of time that the notification work and then one day the dont' work anymore and to fix it i have to reinstall it again, so to investigate i have create this method that it's called when the app did become active:
-(void)checkNotification {
for (UILocalNotification *someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
NSLog(#"%#",someNotification.alertBody);
NSLog(#"%#",someNotification.fireDate);
}
}
and i see that all notification are scheduled and also the date, this is an example:
2012-11-25 18:36:36.532 TestApp[672:907] This is a notification.
2012-11-25 18:36:37.482 TestApp[672:907] 2012-11-27 10:00:00 +0000
so i can't understand why i don't receive notification...any help?
Edit:
i create a notification in this way:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = myNewDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"This is a notification.";
localNotification.soundName = #"smallBell.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
You may or may not get the alert message. It depends on whether your app is running and in which state, e.g. active in the foreground, active in the background, or not running at all.
Please see this article for clarification - http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html
I am using push notification to inform the user that they have a new message. Is it possible to show only the badge and sound when the new message comes without the alert message. Will apple allow to use these kind of notification. If so can any one kind me how to proceed.
Thanks in advance
Sure you can show only badge and sound.
For this what you have to do is:-
where you have set your notification type write:-
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge)];
do not add alert notification.
Give the notification only with sound and batch details dont give the alert notification.
-(void) scheduleNotificationForDate: (NSDate*)date {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
NSLog(#"Notification will be shown on: %#",localNotification.fireDate);
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
FIXED — Ok, found what it was, there was an errant [[UIApplication sharedApplication] cancelAllLocalNotifications]; being fired when I wasn't expecting it.
Well there's your problem.
Thanks for the help everyone, sorry to have it turn out to just be dumb coder syndrome.
I've built out my local notification like so:
- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
NSLog(#"%#", [NSDate date]);
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notif.alertBody = NSLocalizedString(#"Hello.", nil);
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
NSLog(#"Notification scheduled at %#", notif.fireDate);
[notif release];
}
}
As expected my debug log outputs the correct fireDate 10 seconds in the future. If I don't leave my app I get a successful application:didReceiveLocalNotification: callback.
The hiccup here is if I push the button to schedule this notification and hit the home button to put it in the background. If I do this, the notification never fires and I never get an alert view from the OS.
Did I miss something obvious here? I've looked up and down here and the Apple docs and feel I've missed something obvious.
Any help would be greatly appreciated. Thanks.
See the example in Apple's docs:
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
Could it be that not setting timeZone to [NSTimeZone defaultTimeZone] is causing the problem? GMT is assumed if timeZone isn't set (nil default).
Have you tried wrapping the code in a background task?
Ok, found what it was, there was an errant [[UIApplication sharedApplication] cancelAllLocalNotifications]; being sent when entering the background.