UILocalNotification issue with iOS9
Asked Answered
H

4

10

Since iOS9, local notification aren't working properly. Sometimes users receive the notification, sometimes they just don't. My notifications are repeated daily. Any idea what might causing the issue? I saw some posts, that there's a bug in iOS9, but I'm not sure that's the reason.

Here's a piece of code:

    NSDate *alarmDate = [date dateByAddingTimeInterval:DEFAULT_SNOOZE_DURATION * i];  
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];  
    localNotif.fireDate = alarmDate;  
    localNotif.timeZone = nil;          
    localNotif.alertBody = alertBody;  

    localNotif.hasAction = YES;  

    localNotif.applicationIconBadgeNumber = 1;  
    localNotif.soundName = UILocalNotificationDefaultSoundName;  
    localNotif.repeatInterval = NSCalendarUnitDay;  

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]  

Thanks for your help

Heartsome answered 2/10, 2015 at 6:52 Comment(4)
Same problem here, my users are complaining about missed notifications after the IOS9 update.Lacilacie
It looks like new "iOS optimisation" does not allow firing of subsequent notifications.Lacilacie
Did you find a solution for this? I got an app rejection for this very reason. There has been no code change in my project between ios8 to ios9 and it was working in ios8.Baseball
Is there a solution for this problem? I suppose one workaround is to "manually" set additional local notifications rather than use repeatInterval? But we are limited in the number of distinct local notifications we can schedule :-(Hagans
F
5

write below code in AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.

[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{

    [application registerUserNotificationSettings:[UIUserNotificationSettings
                                                   settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
                                                   UIUserNotificationTypeSound categories:nil]];
}

return YES;

}

also write this code in AppDeleagte.m

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Reminder"    message:notification.alertBody
                                                           delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

[notificationAlert show];

// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}

write below code in your ViewController, where you wants to implement your local notification.

forExample : Here I initialise UILocalNotification, and Its Firedate is currentTime and 5seconds Interval Time.

After Fire localNotification it will give alert on Your Screen.

- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.repeatInterval = NSDayCalendarUnit;
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;


[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Fracas answered 27/1, 2016 at 11:11 Comment(1)
Thank you so much, I've been trying for what feels like an internity to make notifications work for iOS<10.0, seems I was missing registerUserNotificationSettings. No mention of it in the official docs, damn Apple!Unchain
P
3

From the Local and Remote Notification Programming Guide:

In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.

As shown in the guide, the sample code that Apple shows is:

 UIUserNotificationType types = UIUserNotificationTypeBadge |
             UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =
            [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

https://mcmap.net/q/1167144/-uilocalnotification-not-working-properly by : Naughty_Ottsel

This is my code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIUserNotificationType types = UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings =[UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    localNotification.fireDate = date;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"Finally got it!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    return YES;
}

I hope that help!

Petey answered 18/4, 2016 at 14:10 Comment(1)
Thanks a lot, Yan. It worked for me. Can we also customize the notification, as in add action buttons?Dugaid
P
0

if You need it for iOS9 but under swift 3.1:

final func registerToGetLocalNotificationsFor_iOS9(){
    let types : UIUserNotificationType = [.sound, .alert /*, .badge*/]
    let mySettings = UIUserNotificationSettings(types: types, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(mySettings)

}

on iOS10 is deprecated.

Purine answered 29/5, 2017 at 11:10 Comment(0)
N
-1

Include the below code in Appdelegate.m

if(IS_OS_8_OR_LATER)
{
    UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
Nano answered 20/11, 2015 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.