How stop local notification sound after cancel local notification - ios

In my application I scheduled local notification with custom sound. After I cancel it from code the sound continue playing. How can I stop it?
-(void)cancelNotification{
if(localNotification){
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
}

Try changing your code like below, hope this works for you. I am adding this just for a try.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[UIApplication sharedApplication] cancelLocalNotification:notification] ;
}

In didRecieveRemoteNotification set your application icon badge number. It will do the trick.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
}

I had the same problem when initially testing with an iPhone 4.
The problem appears to have gone away as of iOS 8 and now Local Notification Sounds stop when canceled after transitioning to the App.
iPhone4 [ iOS - 7.1.2 ] - Local Notification sound keeps playing in App no matter what
iPhone6 [ iOS - 8.1.1 ] - Local Notification sound stop when canceled programmatically
From what I can infer, it appears a fix exists somewhere in iOS 8.x
(Alas I didn't manage to find any documentation or release notes on the matter.)
The Problem becomes a non-issue for apps focusing on iOS 8
(provided you cancel the Local Notification coming in)
I used this same answer for a Similar Problem

Related

Programmatically delete Remote Notifications from Notification Centre

How can we pragmatically remove any pending remote notifications sent for my app from notification centre. I want to clear them up on app launch.
I have tried with [[UIApplication sharedApplication] cancelAllLocalNotifications]; API but its not helping.
PS: This question is specific to iOS 10 and old threads are not duplicates for this one.
Finally...
This one works like charm!
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
You can clear all notifications from notification centre by using these simple lines of code
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
use wherever you want. From my side I have used when user pressed logout.
You can use in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method to clear notifications after app opens
AFAIK, you should do it by using the background mode for remote notifications, and then responding to these notifications by issuing a local notifications.
You can remove local notifications, but not remote notifications.
Reseting the application badge number also remove all the notifications (local & remote) from the notification center.
Objective C
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
Swift
UIApplication.shared.applicationIconBadgeNumber = 0
This can definitely be achieved by using removeDeliveredNotifications(withIdentifiers:) method available in UserNotifications.framework.
For a detailed tutorial, please follow this
https://medium.com/#sebastianosiski/implementing-removable-remote-notifications-on-ios-a17d74832bde

Local Notifications with iOS 8

Local notification are working well in foreground. but I need to perform some background work when a notification banner displays. It is working well when I tap on the banner when local notifications appear.
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSLog(#"AppDelegate didFinishLaunchingWithOptions");
[self application:application didReceiveLocalNotification:notification];
}
return YES;
}
My problem is similar to this issue
I used code below to perform background task:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[[ReminderManger sharedMager] handleNotification:[[notification userInfo] objectForKey:#"reminderId"]];
});
}
There is no way to do that. Notifications are handled by iOS itself. If app would be handling this, there might be some way to get this figured out. I've been wasting my time over it but didn't succeed.
The only thing you can do is to get extended permission from iOS that your app need multi tasking in background. And then in background make your app become post local notifications, this will show banner whenever you want but you can't customize the banner. See Apple's line "An app that is running in the background may also schedule a local notification to inform the user of an incoming message, chat, or update." at this link
Now the thing is in background, you cant change your views, but parse data, and based on this you can make changes at time of launch.

Handling user acting on UILocalNotification

My goal is to open a specific screen when user clicks on a UILocalNotification from the iOS Notification Center.
Now if the app is being resumed from the background when user acts on the Notification, I notice didReceiveLocalNotification is called:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Now I can get the userInfo dictionary from the UILocalNotification object and opens the proper screen.
Unfortunately, this same method is called also when my app publishes a UILocationNotification while the app is running in the foreground:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
How would I distinguish between the 2 cases?
It passes to you the application object that contains the state in its .applicationState property.
Consequently, you can make sure it is not active by testing...
if (application.applicationState != UIApplicationStateActive){
// do your stuff
}
And that's it!

iOS 7 Local notifications with no sound by default

When I run the app on my iOS 7 device, the app doesn't appear on the device notifications settings and doesn't sounds when a notification is being fires. Only after the first notification is being fired, I can see my app under the list of the notification settings with sounds turned off.
Why doesn't the app show in the notifications list initially?
Why are the sounds turned off by default?
On iOS 5&6 I don't have these problems. These are local notifications.
It looks like iOS 7.0.3 includes a fix for that.
Have you set the applicationIconBadgeNumber in "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions"? Comment this code, and try again.....I don't why, but I got the same problem. After I commented this line, my app works correctly.
I got this same problem. What i found out was, i have a piece of code that caused this all along.
In my AppDelegate didFinishLaunchingWithOptions, i have implemented:
//remove this if you have it
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; //to reset the badge everytime the app loads
So what i did, I removed that code & redeploy the app. Now it has ON default value in Notification Center.
Phew.
Hmm,
Interestingly, I changed the order of
notification.SoundName = UILocalNotification.DefaultSoundName;
notification.ApplicationIconBadgeNumber = 1;
to
notification.ApplicationIconBadgeNumber = 1;
notification.SoundName = UILocalNotification.DefaultSoundName;
and it works now. When the app is running in background, the local notification fires and plays the default notification sound.

UILocalNotification is nil after app closes

I am working with UILocalNotifications. A problem I am having is that after I restart my app in XCode the data associated with a notification seems to be deleted. Here are the steps I am following.
-start the app in XCode
-receive the notification
-stop the app in XCode
-start the app in XCode
-go to notification center and tap on one of the notifications for my app
-break in application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
At this point if I inspect notification I find that it is nil.
Any ideas what is going wrong? Thanks.
If a local notification is tapped in notification center then app is started using theapplication:didFinishLaunchingWithOptions: method and you can get access to the UILocalNotification object from the options dictionary by using the key UIApplicationLaunchOptionsLocalNotificationKey.
application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification is only called if a notification fires and the app is in the foreground.

Resources