UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I'm not receiving local notification,can anyone say how to get local notification.
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
Try this!!!!
You need to specify the time when to fire notification. You have just created notification but not set when it need to be fired.
For example..
localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)];
If application is foreground stage then you wan't seen those notification. you should have to code on delegate method.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
UILocalNotification *notif = [[UILocalNotification alloc]init];
notif.alertBody = #"Your Session is Ready";
notif.alertAction = #"Take Now";
notif.soundName = #"DingBell.wav";
[notif setFireDate:[gregCalendar dateFromComponents:dateComponent]];
notif.repeatInterval = NSWeekCalendarUnit;
[notif setTimeZone:[NSTimeZone defaultTimeZone]];
NSMutableDictionary *userDict = [NSMutableDictionary dictionary];
[userDict setValue:self.stringModuleID forKey:#"kRemindMeNotificationDataKey"];
[userDict setValue:[NSString stringWithFormat:#"alarm%d",[[setreminingDay objectAtIndex:i] intValue]] forKey:#"alarm"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Related
I used Notification Alert for my app and used the below code
if(condition){
[defaults setBool:YES forKey:#"notificationIsActive"];
[defaults synchronize];
//self.message.text=#"Notifications Started";
NSTimeInterval interval;
interval = 5;
UILocalNotification* localNotification = [[UILocalNotification
alloc] init];
localNotification.fireDate = [NSDate
dateWithTimeIntervalSinceNow:interval]; //Enter the time here in
seconds.
localNotification.alertBody= #"Alert time";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval=
NSCalendarUnitDay;//NSCalendarUnitMinute;
//Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
else
{
NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;
for (UILocalNotification *localNotification in arrayOfLocalNotifications) {
if ([localNotification.alertBody isEqualToString:#"Alert time"]) {
NSLog(#"the notification this is canceld is %#", localNotification.alertBody);
[[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system
}
}
}
Now problem is I got extra notifications showing this is message Users will see. I checked everything that there is no extra notification alert I added. Now what can I do? Please help..
EDIT:
One point is missing. I used that notification for testing in my app but later I removed that notification.
Three sounds in my project :
}
- (IBAction)Sound1:(NSDate *) fireDate;
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound1.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)Sound2:(NSDate *) fireDate;
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound2.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)Sound3:(NSDate *) fireDate;
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound3.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)SetDatePicker
{
NSDateFormatter *dateFormatter =[ [NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
NSLog (#"Alarm saved: %#", dateTimeString);
[self Sound1:dateTimePicker.date];
[self Sound2:dateTimePicker.date];
[self Sound3:dateTimePicker.date];
}
-(void)scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notifiction =[[UILocalNotification alloc]init];
notifiction.FireDate = fireDate;
notifiction.AlertBody = #"Wake Up!!!";
notifiction.soundName =UILocalNotificationDefaultSoundName;
notifiction.repeatInterval= NSMinuteCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification: notifiction];
}
I want to let the users choose one of them to set it as a Notification sound
I have been searching a lot but i did not found any solution that helped me with
You can specify an audio file for local and push notifications. Allow the user to choose which file they want as the alert sound. Save that preference in NSUserDefaults and then Create a UILocalNotification with the sound.
Example:
You need to include your 3 sound files (Sound1.aiff, Sound2.aiff, and Sound3.aiff for example) in the Xcode project.
- (IBAction)Sound1
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound1.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Sources:
Limits on iPhone push notification sounds?
UILocalNotifications playing Custom sound
https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html#//apple_ref/doc/uid/TP40008194-CH102-SW1
I am using the following code to make a local notification:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:30]; //itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = parentController.activeAssignment.notes;
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
I have checked using the method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
that the notification is firing, but is not showing up on my screen when the app is running in the background.
I want to cancel a UILocalnotification , when i cancel notification still the notification is being fired . I wanted to know do i have to call any delegate method in appdelegate to cancel notification .the code which i am using is executing correctly .but the notification is getting fired .
Should i use NSNotification center which has removeObserver method to cancel uilocalnotification.
Does UILocalnotification fires notification from the app or from the device.
UPDATE - This is how i am scheduling my notification
-(UILocalNotification *)scheduleNotification :(int)remedyID
{
NSString *descriptionBody;
NSInteger frequency;
UILocalNotification *notif = [[UILocalNotification alloc] init];
NSLog(#"%d",remedyID);
descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:#"RemedyTxtDic"];
frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:#"RemedyFrequency"]intValue];
NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];
for (NSDate *fireDate in notificationFireDates)
{
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatInterval = NSDayCalendarUnit;
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
notif.fireDate = fireDate;
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody, #"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
nil];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
return notif;
}
Cancelling notification
- (void)cancelNotification:(int)remedyId
{
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSLog(#"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
for (UILocalNotification *notification in notifications)
{
int notifRemedyId = [[notification.userInfo objectForKey:#"kRemindMeNotificationRemedyIDKey"]intValue];
NSLog(#"remedyID : %d",remedyId);
NSLog(#"notifyId : %d",notifRemedyId);
if (remedyId == notifRemedyId) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
NSLog(#"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);
}
NSNotification center which has removeObserver method to cancel uilocalnotification.
NSNotificationCenter has nothing to do with UILocalNotification. I'm sorry that they both use "notification" somewhere in their names, but that is just coincidence really.
scheduledLocalNotifications will give you the list of all scheduled notifications and use
- (void)cancelLocalNotification:(UILocalNotification *)notification
or you can cancel them all using:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Edit :
NSString *notifRemedyId = #"notifRemedyId";
UILocalNotification *localnotif=[[UILocalNotification alloc]init];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:#"kRemindMeNotificationRemedyIDKey", kRemindMeNotificationRemedyIDKey,YOUR_REMEDYID,#"notifRemedyId",nil];
localnotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localnotif];
Cancel as :
for (UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if ([[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] isEqualToString:#"kRemindMeNotificationRemedyIDKey"]) {
if (remedyId == [[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] intValue]) {
[[UIApplication sharedApplication] cancelLocalNotification:aNotif];
break;
}
}
}
Hope it helps you.
i am trying to get notification when date of array become same as current date this code is i am using for this task
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"dd/MM/yyyy"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
for (int i=0;i<_convertedBdates.count;i++)
{
NSDate *date =[Form dateFromString:[_convertedBdates objectAtIndex:i ]];
NSLog(#"date%#",date);
if(notification)
{
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"New ";
notification.alertAction = #"View";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
suggest me what i am doing wrong
have you put this code in your app delegate?
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Notification Received, %#, set for date %#", notification.alertBody, notification.fireDate);
}
if problem persist then i am here :)
If your NSDate is less than the current date then notification will not be fire. If you are setting notification date on the user birthdate then this will happen.
If your application is in running mode then :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
above method is called.
Hope this will help you.
All the best !!!