iOS 7 Background fetch - ios

I am currently working on one iOS app, in which I am using [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];. According to the docs I have implemented in this way .
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(#"Launched in background %d", UIApplicationStateBackground == application.applicationState);
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
i =0;
return YES;
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(#"Fetch started");
++i;
// Set up Local Notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
localNotification.fireDate = now;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = #"BG Mode";
localNotification.applicationIconBadgeNumber = i;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
completionHandler(UIBackgroundFetchResultNewData);
NSLog(#"Fetch completed");
}
My issue is I have tried to debug it to know how frequently this method is calling . I didn't get any clarification. I have tested with timeinterval 60 seconds, but that is also not working . Please give me any ideas or suggestions . Thanks in advance .

Just to clarify, you've also set the UIBackgroundModes in Info.plist as per Apple's documentation ...
This property has no effect for apps that do not have the UIBackgroundModes key with the fetch value in its Info.plist file.

Related

iOS - Trigger Local Notification when message is received

I'm building an app that allows its users to send each other messages. When a new message is received, it appears in the current user's Table view. I want my app to send the current user a notification when a new message arrives. Does anyone know how I might go about doing this?
I have notifications set up in my AppDelegate already:
appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
and I know the below code will allow me to fire a notification at a specified time (e.g. set by a picker):
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"You are being notified!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
How do I make a notification occur however when a new message is posted to the server (and appears in the tableview) for the logged in user?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];}
If your system is iOS8, you need to register permission.
UILocalNotification * notification = [[UILocalNotification alloc] init];
NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:Time];
if (notification!=nil) {
notification.fireDate= pushDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = kCFCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody = #"Time O.K!";
NSLog(#"Ready");
notification.applicationIconBadgeNumber = 1;
//notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;
NSDictionary * inforDic = [NSDictionary dictionaryWithObject:#"name" forKey:#"key"];
notification.userInfo =inforDic;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
You can have a try,this code I can successfully use on my device.

UILocalNotification doesn't fire in didEnterRegion

I have implemented the
didEnterRegion
method and it works in background for example if I show an alertview...even with the app in background (when I foreground the app the alertViews show)
I use alertview only to test the didEnterRegion method in background and it works...but now im trying to fire an UILocalNotification in didEnterRegion but the UILocalNotification is not firing. Theres my code:
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
int iRegiao = [region.identifier intValue];
Location *ocorrencia = (Location *)[self.locations.objects objectAtIndex:iRegiao];
// NSLog(#"Voce está proximo da %#", ocorrencia.name);
// UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
notification.fireDate = [NSDate date];// Now here you can manage the fire time.
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = ocorrencia.name;
notification.alertAction = ocorrencia.name;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Are you targeting iOS 8? If so, you need to ask for permission for local notifications.
Try this:
if ([[UIApplication sharedApplication] respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
}
The respondsToSelector: check is needed if you are also supporting iOS 7 or below.
Did you try to use - (void)presentLocalNotificationNow:(UILocalNotification *)notification on UIApplication? (it ignores fireDate)

How to remove local notification after fired notification in IOS

I am working on a project where I fire a local notification frequently also repeat notification.
After fired notification, if I delete the application and install it again (without opening it), I get all the old notifications.
- (void)applicationWillTerminate:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
*and then set remaining all notification in*
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UILocalNotification *notif = [[UILocalNotification alloc]init];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
Try this:
-(void)clearNotifications
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
put your code in App did finish launching method.so, when app launch then it's cancel the all notification.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//cancel All notification when app is launch
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
Pls note that it cancel the all notification. if u want to cancel any particular notification then set it's tag and using tag cancel this notification.

Premeditated Notifications

Is it possible to setup an app to send notifications from it's self at specified times? Perhaps by putting them in a queue? As of now I'm having to go through a push notification server and that seems like over kill when I'm sending messages out every Tuesday morning. Also, what is this called (so I can better research it on the web)
You can schedule notification like this:
- (void)scheduleNotification
{
//Cancel all previous Local Notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//Set new Local Notifications
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
UILocalNotification *notif = [[cls alloc] init];
//3 days
notif.fireDate = [NSDate dateWithTimeInterval:60.0f*60.0f*24.0f*3.0f sinceDate:[NSDate date]];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Come Back Again! Lets blast All Zombie!";
notif.alertAction = #"PLAY";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
[self scheduleNotification];
..
}

Is it possible to get Local Notification when application's state is inactive

I'm developing an iOS application, and I have to get local notification, BUT in case of application's state is inactive. I successfully get notifications when application is in background state.
So, is it possible to get local notification, when application is inactive?
Or maybe that's possible only by using push notification?
Regards,
Armen
You need to respond to local notifications in two places in your app delegate:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
The first is for when your app was not running - use the launchOptions parameter to check if your app was launched due to a local notification.
The second is for when your app is currently running (active or inactive). You can check if the app is inactive by checking NSApplication's applicationState property in the application:didReceiveLocalNotification: method.
- (void)sendNotification
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// notification.repeatInterval = NSDayCalendarUnit;
localNotification.fireDate = vwPicker.date;
localNotification.alertBody = txtAlarmTitle.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.userInfo = #{#"Title": txtAlarmTitle.text};
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[self handleNotification:notification application:application];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification)
[self handleNotification:localNotification application:application];
return YES;
}
-(void)handleNotification: (UILocalNotification *)notification application:(UIApplication *)application
{
NSString *title = [notification.userInfo objectForKey:#"Title"];
[[[UIAlertView alloc]initWithTitle:#"Smart Alarm" message:title delegate:self cancelButtonTitle:#"Answer the Teaser" otherButtonTitles: nil] show];
application.applicationIconBadgeNumber = 0;
}
Sure, just use willResignActiveNotification notification listener as listed
here

Resources