I am a little bit confused if i have to do that,or the system :
Using the iBeacon there is a method that is fired when app is in background or closed.
There , i would like to write the code that will show the user a push notification on the iPhone home screen, to let him know about it .
How would i do such a simple thing ?
//happens in background when user inside a place
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
NSLog(#"***********************ENTER");
//here show notification !
You could try something like this. This will schedule a local notification to fire at a set date and time (you need to provide the NSDate and message).
...
NSDate *fireDate = // whatever date and time you want to fire the notification
NSString alertMessage = #“Alert message!";
[self addNotification:fireDate mymessage:alertMessage];
...
-(void)addNotification:(NSDate *)mydate mymessage:(NSString *)mymessage
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = mydate;
localNotification.alertBody = mymessage;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Related
When I create my local notification callback didReceiveLocalNotificationgets triggered. The same callback gets triggered when I click on the local notification. Currently I was dividing those two cases by checking
if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
//this means notification is clicked
}
But the main problem here is that when you are in the foreground and you slide your notification menu, and then receive your local notification, this callback didReceiveLocalNotification gets called. And in this case my app goes into this if. Because of this, I can't really distinguish from clicking the notification and creating a local notification while app is in the inactive state. Any ideas on how can I fix this?
This is the code for scheduling a local notification:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"aaaa";
localNotification.alertTitle = #"title";
localNotification.userInfo = myUserInfo;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
After calling this, I get didReceiveLocalNotification delegate triggered.
I had to work around the exact same issue in my app. I couldn't find an offical way in the API to tell the difference, but here is a workaround.
Pass the current date in the userInfo when the local notification is created:
localNotification.userInfo = ["alertDate": NSDate()]
And when you handle didReceiveLocalNotification, check against current date again, to make sure it didn't just fire just a moment ago:
if application.applicationState == .Inactive {
if let alertDate = notification.userInfo?["alertDate"] as? NSDate
where (NSDate()).timeIntervalSinceDate(alertDate) > 0.1 {
// this means notification was initiated by user
}
}
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = #"image inserted";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self dismissViewControllerAnimated:YES completion:nil];
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 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!
I'm working on an app that alerts the user when he is close to some landmarks using the region monitoring. Everything works fine but when the app is in the background I don't get the alerts. When I open the app I get all the alerts popping up. What I wanted was to get them when the app is in the background. I'm wondering if it's possible or does the app needs to be running to get alerts? Any help would be greatly appreciated.
Update:
The problem seems to be that I used Alerts instead of local notifications. Here's the code I used:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(#"Entered Region - %#", region.identifier);
[self showRegionAlert:#"You are near: " forRegion:region.identifier];
}
How can I change this to local notifications?
Check the "testing your apps region monitoring" section in
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html
If you switch back and forth between foreground and background the threshold conditions might not be met and trigger before you bring the app to the foreground again.
Also when the backgrounded app gets a notification there is only a small window for processing the message. Trying to do network requests might time out...
Check your plist settings - declare location as UIBackgroundModes only required if you need high precision positioning. The significant location changes works even without location defined.
Check that locationManager:didUpdateLocations: and locationManager:didFailWithError: are being called and no errors are posted.
Check that you haven't set ApplicationRunsInBackground to NO in your plist.
Try implementing the AppDelegates applicationDidEnterBackground:, application:didFinishLaunchingWithOptions: and friends to spot where in the app lifecycle you are at a given time.
For geofencing solution, you can refer the answer given by #Niels Castle.
For local notification, you can refer below code:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"I'M IN THE REGION";
localNotification.userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:#"nearBy",#"type", nil];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(#"Yes! Welcome to %#",region.identifier);
UILocalNotification* notify = [UILocalNotification new];
notify.alertBody = [NSString stringWithFormat:#"Welcome to %#",region.identifier];
notify.soundName = UILocalNotificationDefaultSoundName;
if (notify.applicationIconBadgeNumber == 0) {
notify.applicationIconBadgeNumber = 1;
}
[[UIApplication sharedApplication] presentLocalNotificationNow:notify];
}
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