I am working on Alarm module in my app. I have develop code which take date and time input from user and set Event on that particular time in my app.
Also i store information into my local database to display list of alarm set by user.
Now i want to delete entry from database when particular alarm executed (When UILocalNotification displayed into app i want to call database method to delete that entry from db)
I set Notification by this way
NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
int notificationCount = [preferences integerForKey:#"notification_count"];
notificationCount++;
NSDate* final_date = [calendar dateFromComponents:final_Components];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = final_date;
localNotification.alertBody=titleTextField.text;localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSDictionary* userInfoDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:notificationCount] forKey:#"notification_id"]; localNotification.userInfo = userInfoDic;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[preferences setInteger:notificationCount forKey:#"notification_count"];
I used Delegate didReceiveLocalNotification
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *itemName = [notification.userInfo objectForKey:#"notification_id"];
NSLog(#"userInfo is %#",itemName);
databaseObject.deleteNotification(itemName)
}
My problem is "didReceiveLocalNotification" only call in 2 case
1) When user using app (app in foreground).
2) when notification displayed and user click on notification
But in 3rd case when app is in background mode and notification displayed and if user don't click on notification Or second case is open app directly clicking on app icon or user clear notification at that time didReceiveLocalNotification delegate is not get called..
Is there any way to detect Notification fire in all case or any other method by using i can detect that notification has been fire and then i will execute my delete method.
Any help is appreciated
Thank you
Yes, you are right, you won't know the information of the notification if your notification fires while your app is suspended/terminated and user didn't tap the notification to launch/active your app.
The only way to do this is to calculate the time every time your app is running and reschedule notifications and update your database.
For millisecond calculation:
NSInteger myMillisecond; //assume it exists
const NSTimeInterval oneSecondAsMilliseconds = 1000.0;
NSTimeInterval myTimeInterval = myMillisecond/oneSecondAsMilliseconds;
NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimeStamp = [currentDate timeIntervalSince1970];
if (myTimeInterval > currentTimeStamp) {
//myTimeInterval is a later time than now
}
Related
I have simple UILocalNotification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Message";
notification.alertAction = #"Action";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.category = kCategoryIdentifier;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Is it possible, to repeat notification once, for example after two minutes? I want behaviour exacly, like in Messages app.
I have tried to set repeatInterval property of notification object, but:
Notification will be presented to user every two minutes, not repeated only once
System shows to user new notification, not repeat the old one. User see two notifications, one with timestamp 2 minutes after another.
Which is not what I've expected.
Also, because of second reason, I don't want to schedule two separate notifications.
Edit: In my app time when something happend is very important. Because of that, in lock screen, when notification is repeated, I want user to know that is something that happend earlier, not in time when notification arrives. So repeated notification should have timestamp of first notification.
Yes, you can set repeatInterval.
See documentation here
The calendar interval at which to reschedule the notification.
Declaration SWIFT var repeatInterval: NSCalendarUnit OBJECTIVE-C
#property(nonatomic) NSCalendarUnit repeatInterval
try this code
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"Message";
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber=1;
NSLog(#"LocalNotif.soundName %#",localNotif.soundName);
for (int i=0; i<20; i++)
{
localNotif.fireDate = [repeatAlarm dateByAddingTimeInterval:120*i];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
I'm not sure if an iOS app can post a notification from the background without internet connection? (so this is not a push notification, just to post from phone)
Example: An iOS app that plays music in background can prompt user how long the user has listened to music from background.
I think you should use local notification, please see the below code for local notification,
NSDate *alertTime = [[NSDate date]
dateByAddingTimeInterval:10];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertTime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = #"bell_tree.mp3";
notifyAlarm.alertBody = #"Staff meeting in 30 minutes";
[app scheduleLocalNotification:notifyAlarm];
}
With these requirements in the mind, the following code creates an NSDate object based on the current date and time plus 10 seconds. This date object is then used to schedule a notification with no repeats, a text message and the sound from the audio file
The local notifications in iOS can help you to fix the problem. May be this tutorial helps you.
I have made one sample application which fires local notification.
When notification fires it always shows banner in notification area in device, which I have shown in image.
But I want alert rather than this and want to perform action based upon selected option from that alert.
Code to fire local notification is given as below.
-(IBAction)setNotification:(id)sender{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
{
return;
}
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Get the year, month, day from the date
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSTimeZoneCalendarUnit|NSSecondCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:[NSDate date]];
// Set the second to be zero
components.minute = components.minute + 1;
components.second = 0;
// Create the date
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:components];
NSLog(#"Fire Date :: %#",date);
localNotif.fireDate = date;
localNotif.alertBody = [NSString stringWithFormat:#"First Alarm"];
localNotif.alertAction =#"Ok";
localNotif.soundName=#"Alarm_1.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.alertAction = #"Application name";
localNotif.HasAction = true;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Can any body please tell me if there is any mistake.
Thanks in advance.
Here is a quick and short answer. You cannot do that.
Apples documentation only states didReceiveLocalNotification. The way the notification is shown is not up to the developer. The user will choose how he wants to see notification using SETTINGS.
In your case, just wire up logic by implementing the delegate callback when the user taps on the notification.
Change the Type of Notification in Setting -> Notification Centre - > Your App -> Alert:
Originally from Quinn "The Eskimo!", as quoted by IBG:
"This depends on you mean. You have some control over how the
notification appears based on how you set the UILocalNotification
properties (things like alertBody, soundName, and so on). However, if
you're asking about the way in which those properties are interpreted
(the things the user can customise in Settings > Notifications), those
are user preferences and not exposed via any API."
You can get your notification in this method:
(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
write this code for fetch the data from userInfo:
[[userInfo objectForKey:#"aps"] objectForKey:#"price"]];
use userInfo Dict to get The notification Value and after that you can use that data for Alert.
I am trying to run some function every n-minutes when app is not running.
I thing local notification is suitable for that, but I have one problem.
If I set local notification as :
-(void)notification{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:15];
//localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"marko";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
The notification is fired every time and then do:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSLog(#"Recieved Notification %#",notif);
}
What I would want is to after 15s first fire some function and if function return YES then play sound and put badge on it.
How to do that?
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it.
Make sure you are testing in device.
I want to cancel an iOS local notification after a particular time.
For example : a week later
- (void)ViewDidLoad
{
NSDate *date = [NSdate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
//set some localNotif's properties
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.fireDate = date
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
How can I cancel localNotif after a week (7 days) and can you show me the code?
You can't do that unless your app is running in the foreground at the time the notification is due to be cancelled (in which case there would be no need to cancel it anyway).
The reason you can't do it is because you would need a timer to tell you when to cancel it, and you can't schedule a timer unless you are an app that has a background mode, in which case you could schedule a timer to notify you - but even background apps can be suspended still it would not be guaranteed.
See here
iOS Run Code Once a Day