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.
Related
I have created a UILocalNotification I would like to know how ever what repeateCalendar can be used for?
This is my simple implementation of a UILocalNotification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:dateFromString];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
// Setup alert notification
localNotification.alertTitle = [NSString stringWithFormat:#"Reminder (%#)", amPm];
localNotification.alertBody = [NSString stringWithFormat:#"This is my alertBody"];
localNotification.userInfo = identificationDict;
localNotification.repeatInterval = NSWeekCalendarUnit;
//localNotification.repeatCalendar = what do I put here? and why;
localNotification.soundName=UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
Currently Apple docs say
The calendar the system should refer to when it reschedules a
repeating notification
However no discussion on why you would use it? In theory could I use it to define Weekday and Weekends?
Other than the most widely used calendar - Gregorian calendar, there are a few other calendars, for example in China, we have a Chinese calendar (NSCalendarIdentifierChinese) which is totally different from the Gregorian calendar. We use that calendar for our traditional holidays and astronomical dates. So if the users are mainly Chinese, then you probably need to set repeatCalendar to Chinese calendar, otherwise your notification might fire on the wrong date. Here is the docs from Apple talking about all different kinds of calendars.
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 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
}
I have an app that uses UILocalNotification to set a repeating notification. When the user gives the app permission, I set a notification in the AppDelegate.m to repeat at 9pm every night but it's not firing consistently.
As far as I can tell, it seems like the notification will fire if the user opened the app at some point during the day (i.e. after the last notification fired at 9pm yesterday and before 9pm today when the next one fires), but when they don't open the app all day, no notification arrives.
I've tried cancelling all notifications in didFinishLaunchingWithOptions before creating a new one, and not cancelling and recreating them. I don't know what else to try or why the OS isn't handling this for me. I noticed when using other to do list apps on my phone that often the badge count doesn't update for the day until I open the app. Is this an iOS limitation? If so, why bother using repeatInterval if the OS can't handle it for you unless your app gets opened anyway?
Here's the method I use for creating my notification:
- (void)createLocalNotification {
// Create local notification
self.localnotif = [[UILocalNotification alloc] init];
self.localnotif.alertBody = #"Time to rate your day!";
self.localnotif.alertAction = #"Rate";
// Set a date of today for the date components
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [[NSCalendar autoupdatingCurrentCalendar]
components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
| NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
// Set the date components time to 9pm for notification
dateComponents.hour = 21;
dateComponents.minute = 0;
// Pass in userInfo dict
self.localnotif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:#"mood rating", #"notification", nil];
NSDate *fireDate = [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:dateComponents];
// Set fireDate for notification and schedule it
self.localnotif.fireDate = fireDate;
self.localnotif.timeZone = [NSTimeZone systemTimeZone];
// Make the notification repeat every day
self.localnotif.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:self.localnotif];
}
I call this method from didFinishLaunchingWithOptions in my AppDelegate.m like this:
if ([UIApplication sharedApplication].scheduledLocalNotifications.count >= 1) {
// handle notification here
} else {
[self createLocalNotification];
}
One thing that might be related in iOS 8, I think you need to register for notifications for local notifications. They will show up within the app if you haven't registered but will fail outside the app.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
I need to send a text message when the current time equals the time selected in the UIDatePicker. How might I do this? You don't need to include the code to send the message, I already have that coded. I've tried all sorts of things with NSTimer and if - then statements but none have worked.
Edit: Since I wrote this question I've found a better way to do things. I just need to set a local notification and when received execute my code with -(void)didRevieveLocalNotification. Here is what I have so that any googlers can hopefully be helped.
NSDate *pickerDate = [self.datePicker date];
//Set Local Notification
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = pickerDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
//----------------------------------------------------------------------
notif.alertBody = #"Tap to send your text message!";
notif.alertAction = #"send message...";
notif.soundName = #"sms_alert_nova.caf";
notif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
well i would use a local notification... something like this
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = theDate //The date that your picker has selected
notification.alertBody = #"Hey, the time just expired!"
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Then in your AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//Code to manage the notification logic
}
Hope this helps, the user will get the alert even if on background.. if on background the user must click the alert to let your application know that the local notification triggered, if he does (or he is on your app already, then the app delegate method will trigger letting your app know that the notification fired...
Hope this helps!