UILocalNotification not firing when the app is in

2019-02-26 02:01发布

问题:

There are quite a few questions on why local notification is not firing properly here and there,also there are several questions on why local notification is not firing when the application is in background state which I have also gone through them.

But to my surprise I haven't found any notification post related to foreground state or in active state,i.e. in my app I am facing this strange issue,i.e. local notification fires when the app enters background mode and it doesn't fire when the app is in active state or in foreground mode,to my surprise again,even after fire date set for notification is expired,immediately after entering background,the notification fires.

EDIT

Also another issue I am facing is the alert is not firing,i.e. the alert action we write in didReceive local notification method,here is the implementation code:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo objectForKey:addViewController.textField.text];
    [self.addViewController showReminder:reminderText];
}

And here is that showReminder method which is present in another controller i.e.:

//Notification alert
- (void)showReminder:(NSString *)text
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder" message:text delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];

    UIImage *image= [UIImage imageNamed:@"icon@2x.png"];
    [imageView setImage:image];

    [alertView addSubview:imageView];
    [imageView release];

    [alertView show];
    [alertView release];
}

Sorry if this issue doesn't require a question or post in stackoverflow.

Any one please post your suggestions and any help is gladly appreciated!

Thanks all in advance :)

回答1:

If the application is active then you will not recieve any sound, badge or alert, however the application delegate application:didReceiveLocalNotification: will be called

From apple docs

If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.