Receive local notifications after deleting and rei

2020-06-09 06:50发布

I am using UILocalNotification in my project. I am stuck with an issue using the UILocalNotifications. If I schedule notifications for a week, delete the app and re install with no notification scheduled from the re-installed app, I receive the notifications for the times that were scheduled previously.

Even if there are no notifications scheduled from the present install, I receive the notifications. Is there a way to unschedule/remove these notifications?

1条回答
够拽才男人
2楼-- · 2020-06-09 07:53

Actually, when you schedule future notification, then delete app and then again re-install it, in this case you will receive previously setted notification. Which you are getting.

Solutions:

When you open the app then in "didFinishLaunchingWithOptions" method of AppDelegate, call below method.

-(void)removeAllLocalNotification
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

So that, you will remove all previously set notification.

But, before doing above thing: You need to take care that, you have to call the above method only once. Not every time when app launch.

You can do it in following way:

Create one BOOL variable and store it in NSUserDefault. Now, when app open then check it's value from NSUserDefault. If it is FALSE, then call above method and set it's value to TRUE and set into NSUserDefault.

Now, when you re-open the app then you will get it's value as TRUE so at this time, you don't need to call above method. So that, your current set notification not removed.

Hope, you got the entire things.

Happy Coding.

Cheers!

查看更多
登录 后发表回答