-(void)setNoti {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"NOTI 1";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.repeatInterval = kCFCalendarUnitMinute;
NSDictionary *dict = #{#"nid":#"1"};
localNotification.userInfo = dict;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
Why my simple UILocalNotification is not firing?
Have you added this code in didFinishLaunchingWithOptions of AppDelegate
UIUserNotificationType types = (UIUserNotificationType) (UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
I am not sure you added or not, but if you missed this, you will never get local as well as remote notification.
You have To Register Notification setting For Local and Remote Notification both Please Register notification setting First.
Related
I created a Local Notification that triggers 60 seconds when a certain button(SetButton) is clicked. My problem right now is if SetButton is pressed again, it does not override the first press, it displays 2 notifications and so on. How do I make sure that the second press of the button overrides the first press and there isn't a build up of notifications ?
- (IBAction)SetButtonPressed:(id)sender {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"HEY GET UP";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
My AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ([UIApplication instanceMethodForSelector: #selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
if (localNotification) {
application.applicationIconBadgeNumber = 0;
}
}
If you only use notifications for that specific action you could cancel all notifications at once using
[[UIApplication sharedApplication] cancelAllLocalNotifications];
It looks like you only have one type of local notifications in your app.
In that case you could keep it simple. Whenever you want to schedule a new one - cancel the previous one.
- (IBAction)SetButtonPressed:(id)sender {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"HEY GET UP";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber =
[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
I am stuck with the strange problem. I am making an alarm application.When i fire a local notification it works fine in simulator but when I compile the code in an iPhone it dose not working.Same code is working on an simulator and not responding on a iPhone.
notification code:
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = fireDate;
localNotif.alertBody = #"Time to wake Up";
localNotif.alertAction = #"Show me";
localNotif.soundName = #"Tick-tock-sound.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
and save button code is:
- (IBAction)saveBtn:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
NSLog(#"time is %#",dateFormatter.timeZone);
dateFormatter.timeStyle = NSDateFormatterShortStyle;
NSString * dateTimeString = [dateFormatter stringFromDate:timePicker.date];
datesArray = #[[dateFormatter stringFromDate:self.timePicker.date]];
}
Please tell me the solution. Thanks
Write this code in appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
return Yes;
}
I am working on VOIP iOS app in which i have to show a local notification when call comes and app is in background mode i make it like this
Register Notifications in appdelegate
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
showing Notification
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
UILocalNotification* localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertTitle = #"Call From";
localNotif.alertBody = [NSString stringWithFormat:#"%#", callerName];
localNotif.alertAction = #"Receive";
localNotif.applicationIconBadgeNumber = 1;
localNotif.fireDate = [NSDate date];
localNotif.userInfo = #{#"name":callerName};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}
the local notification show as alert on top but when user not attend the call or other user disconnect the call, local notification removed automatically from Notification centre which is very weird, i googled too much but no success suggest some solution.
Thanks in advance.
The code to register for notifications is:
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
And the code to schedule the notification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
[self setNotificationTypesAllowed];
if (notification)
{
if (allowNotif)
{
notification.timeZone = [NSTimeZone defaultTimeZone];
if ( [statusString isEqualToString:#"daily"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitDay;
}else if ( [statusString isEqualToString:#"weekly"]) {
notification.fireDate = _timePick.date;
notification.repeatInterval = NSCalendarUnitWeekOfYear;
}else if ( [statusString isEqualToString:#"fortnightly"]) {
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60*24*14];;
notification.repeatInterval = NSCalendarUnitMinute;
//notification.repeatInterval = NSCalendarUnitYear;
}else{
notification.repeatInterval = 0;
}
}
if (allowsAlert)
{
notification.alertBody = [NSString stringWithFormat:#"Do you really want to send message to %#",name];
}
if (allowsBadge)
{
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
if (allowsSound)
{
notification.soundName = UILocalNotificationDefaultSoundName;
}
notification.alertAction = #"Yes";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showSMS) name:#"showSMS" object:nil];
// this will schedule the notification to fire at the fire date
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
I am able to repeat local notification daily & weekly but it is not repeating fortnightly ,please help
Yes as #Nikos suggested, try below code:
Cancel UILocalNotification.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Schedule local notification
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.userInfo = #{#"notification_identifier":#"After14Days"};
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)];
notification.alertBody = #"Text to display";
notification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
If you have any doubt then let me know.
To set a notification to fire every 14 days you have to create 26 separate UILocalNotifications with the repeatInterval set to NSYearCalendarUnit (the repeat interval must be set to a calendar unit, and there is no 14 days CalendarUnit in iOS).
Another way to handle it is to cancel the UILocalNotification in the didReceiveLocalNotification method and schedule a new one there for after 14 days. This method though assumes that the user interacts with the notification, otherwise the next one will never get scheduled.
When my app goes in background then applicationDidEnterBackground is automatically called and in this method we fire local notification. but didReceiveLocalNotification: method is not called
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = #"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}
Your expectations with local notifications are incorrect:
When your app will go in background then you will see an immediate notification on your phone but you have to tap that notification to trigger didReceiveLocalNotification delegate.
If you receive local notification in foreground then didReceiveLocalNotification will be triggered automatically.
Above scenario is tested and verified.
Update: You must read this documentation:
http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html
Try this code:
NSDate *pickerDate = [date_picker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = #"Alert Body Message";
localNotification.alertAction = #"Alert Action Message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
and use this code in didFinishLaunchingWithOptions in App delegate class.
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
Before scheluding notification you must register it for specified type:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
bgTask = UIBackgroundTaskInvalid;
}];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = #"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
// register notification settings
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];
#endif
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}