App lost control over local notifications after be

2019-02-11 17:07发布

问题:

I have an app with local notification at the App Store.

In the last 4 version updates to my app, there were customers who experienced something strange - the app lost control over previous notifications, they kept getting all notification scheduled before the update, even though they should've been canceled by [[UIApplication sharedApplication] cancelAllLocalNotifications]!

While communicating with these customers, I told them to turn off reminders, something that performs cancelAllLocalNotifications, but they claim to keep getting reminders.

Also, reinstalling the app doesn't get rid of the old notifications! It's as if iOS thinks this is a different app.

It doesn't happen to most of my customers, only to rare few - but still, it happens every update!

How can it be?

Update

3 years later and it's still happening.

Every update some customers keep receiving local notifications, but the app can't see those, nor can control them.

Can anyone solve this or prove it is a bug of iOS?

回答1:

Did you check How does UIApplication cancelAllLocalNotifications work between app updates? It doesn't have definitive answers, but maybe it would be useful.

Try to find out what's so special about the customer that experience this:

  • Is it always the same customers who experience this behavior?
  • Which iOS version they use?
  • How are they using you app differently? Have a lot of notification scheduled?
  • How are they making the update? (over-the-air, sync, etc.)


回答2:

I had similar issues with notifications. My solution to actually delete was to set a specific date and only notify once. The line applicationIconBadgeNumber = 0 removes the notification from notifications list. The badge number is only set when the user entered the app. For the case when the user didn't see the message yet you can just add a check in applicationWillEnterForeground and display a proper UIAlertView with the same message as the notification, and you can get the last notification in a similar way by using notificationArray. When you need to set a new notification "isNotified" needs setValue:@"0" and be synchronized. When I use multiple notifications I save states like this:

[userDefaults setValue:@"1" forKey:[NSString stringWithFormat:@"notification-%@", @"12"]];
NSInteger number = [[userDefaults objectForKey:[NSString stringWithFormat:@"notification-%@", @"12"]] integerValue];

In MainViewController.m

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger alreadyNotified = [[userDefaults objectForKey:@"isNotified"] integerValue];

if (alreadyNotified == 0) {

    NSInteger seconds = 60;
    NSString *message = @"Just testing!";

    UILocalNotification *notification = [[UILocalNotification alloc] init];

    UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    BOOL allowNotif = (currentSettings.types != UIUserNotificationTypeNone);
    BOOL allowsSound = (currentSettings.types & UIUserNotificationTypeSound) != 0;
    BOOL allowsBadge = (currentSettings.types & UIUserNotificationTypeBadge) != 0;
    BOOL allowsAlert = (currentSettings.types & UIUserNotificationTypeAlert) != 0;

    if (notification)
    {
        if (allowNotif)
        {
            NSDate *now = [NSDate date];
            if (seconds > 0) {
                now = [now dateByAddingTimeInterval:seconds];
            }
            notification.fireDate = now;
            notification.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
            notification.repeatInterval = 0;
            NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", message, @"message", nil];
            notification.userInfo = userInfo;
        }
        if (allowsAlert)
        {
            notification.alertBody = message;
        }
        if (allowsBadge)
        {
            notification.applicationIconBadgeNumber = 1;
        }
        if (allowsSound)
        {
            notification.soundName = UILocalNotificationDefaultSoundName;
        }

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];

        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setValue:@"1" forKey:@"isNotified"];
        [userDefaults synchronize];
    }

} else {

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *notificationArray = [app scheduledLocalNotifications];
    for (int i=0; i<[notificationArray count]; i++)
    {
        UILocalNotification *notification = [notificationArray objectAtIndex:i];
        [app cancelLocalNotification:notification];
    }
}

In AppDelegate.m

- (void)applicationWillEnterForeground:(UIApplication *)application {
   application.applicationIconBadgeNumber = 0;
}


回答3:

Add the following code to your application:didFinishLaunchingWithOptions: method to clear all the notification.

 [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
 [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
 [[UIApplication sharedApplication] cancelAllLocalNotifications];

Once done you can add the new notifications.



回答4:

Hey that is what is also happening with my app as well. My local notifications are getting fired in below scenerios :
1. user has deleted the app

  1. Using same app on same device but with different user name, they getting alerts for last logged in user.

-Actually its UILocalNotification behave, we have to cancel them else IOS retain them for at least some time(not sure how long) when app is deleted and they were scheduled by the app at deletion time and were not canceled.

So always make sure when ever your app install happens,keep a check & cancel all scheduled notifications.

like

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    if(//its a fresh install){

     [[UIApplication sharedApplication] cancelAllLocalNotifications];

    }


    }

cancelAllLocalNotifications will cancel all existing notifications.

Hope it will help.