Local Notification Not Firing With Background Fetch - ios

Can someone please take a look at the following code and tell me why the local notification isn't firing. Im running the app in XCode and using the debug option to simulate a background fetch but the local notification doesn't fire.
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"performFetchWithCompletionHandler");
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = #"Update demo text!";
localNotif.alertAction = #"OK";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.fireDate = nil;
NSLog(#"Local Notification");
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
//Perform some operation
completionHandler(UIBackgroundFetchResultNewData);
}

Do you query the user to allow push?
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}

Instead of checking device version, use below code that checks RespondToSelector:
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)])
{
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

Related

UILocalNotification not called when is in background

I'm try to fire UILocalNotification when app is in background and is in active state. I use following:
In App Delegate i want to "catch" notification callback by this (it's not called):
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
NSLog(#"recieve-old-notif-here");
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(#"recieve-old-notif");
}
Here is how i declared local notification:
NSString *strToShow = [NSString stringWithFormat:#"Время вставать"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
notification.alertBody = strToShow;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Please note that i already achieve that task with iOS 10 notifications, but i want to support this feature on older version devices.
So, my delegate methods suppose to call but their does not, why?
Add below code to the didFinishLaunchingWithOptions method in delegate :
ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
endif
and below delegate methods;
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:#"declineAction"]){
}
else if ([identifier isEqualToString:#"answerAction"]){
}
}
#endif

Reminder is not working

I want to remind the user based on time,but cannot achieve using this code,please give me any solution.This is my code:
NSDate *pickerDate = [self.StartDate date];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
NSDate *date1=[pickerDate dateByAddingTimeInterval:60];
notifyAlarm.fireDate = date1;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
//notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval =NSCalendarUnitWeekday;
notifyAlarm.soundName =UILocalNotificationDefaultSoundName;
notifyAlarm.alertBody =self.EventText.text;
//notifyAlarm.alertLaunchImage=#"in.png";
[app scheduleLocalNotification:notifyAlarm];
[self dismissViewControllerAnimated:YES completion:nil];
Write this code in didFinishLaunch method in appdelegate.m
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}

local notification in an application

I am stuck with the strange problem. I am making an alarm application.When i fire a local notification it works fine in simulator but when I compile the code in an iPhone it dose not working.Same code is working on an simulator and not responding on a iPhone.
notification code:
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = fireDate;
localNotif.alertBody = #"Time to wake Up";
localNotif.alertAction = #"Show me";
localNotif.soundName = #"Tick-tock-sound.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
and save button code is:
- (IBAction)saveBtn:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
NSLog(#"time is %#",dateFormatter.timeZone);
dateFormatter.timeStyle = NSDateFormatterShortStyle;
NSString * dateTimeString = [dateFormatter stringFromDate:timePicker.date];
datesArray = #[[dateFormatter stringFromDate:self.timePicker.date]];
}
Please tell me the solution. Thanks
Write this code in appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
return Yes;
}

UILocalNotification removed automatically

I am working on VOIP iOS app in which i have to show a local notification when call comes and app is in background mode i make it like this
Register Notifications in appdelegate
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
showing Notification
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
UILocalNotification* localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertTitle = #"Call From";
localNotif.alertBody = [NSString stringWithFormat:#"%#", callerName];
localNotif.alertAction = #"Receive";
localNotif.applicationIconBadgeNumber = 1;
localNotif.fireDate = [NSDate date];
localNotif.userInfo = #{#"name":callerName};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}
the local notification show as alert on top but when user not attend the call or other user disconnect the call, local notification removed automatically from Notification centre which is very weird, i googled too much but no success suggest some solution.
Thanks in advance.

I want to set local notification in my app

I m new to objective-c, i want to set local notification in my app.
App should be work with both IOS 8 and all less than IOS 8 version.
please help me with detail code.
Try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
#end
}
and you fire a local notification like this:
- (void)showNotificationWithMessage:(NSString *)message
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = message;
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Resources