Supress localNotification after an alertView is shown - ios

In my app a distance, set by the user, is watched. When the distance is covered an AlertView dialog is shown and a sound is played. By tapping the OK butten the alertView dissapear and the sound stops. When the app moves to the background the method UILocalNotification takes care of the sound and the message. This works ok. But...
When the app is in the foreground and the AlertView is shown and dismissed, the UILocalNotification method stays active. Thus when the app moves to the background it "plays" the local notification with the sound (again).
How could I avoid that behavior?
I want the message played once. In the foreground OR in the background. Not both. I've tried to solve this problem with an if() statement but that gives me unexpected results.
Please advice.

Just call
[[UIApplication sharedApplication] cancelAllLocalNotifications];
when the user dismisses the alert in the foreground.
In case you need to have other local notifications active, just cancel the one in question.

Related

How to show notification enable alert after some delay in iOS?

In my app at the time app the launches first time there is an animation on first UIViewController for only 5 seconds. The problem is when app launches first time animation starts and Notification enable alert pop's up, so animation is hidden behind the notification enable alert and as soon as user tap ok on notification alert; animation ended. Because of this notification enable alert user can't see full animation. Please provide suggestion how to fix this ? Is there any way to delay notification enable alert to show after some delay ?
I'm guessing your requesting authorization for notifications in your didFinishLaunching (or perhaps somewhere else). Where ever you're doing it just move it to the completionHandler of the animation and from there add a 5 second delay using asyncAfter.

Determining app state when `UIAlertView` or `UIAlertController` are displayed and dismissed

When a privacy alert is shown in iOS (for example asking for access to Photos), UIApplicationWillResignActiveNotification is called as the alert is displayed and the app becomes inactive. Then when a choice is made on the alert and the app resumes, UIApplicationDidBecomeActiveNotification is called. This is also the case when other alerts are shown for example when a notifications alert is displayed i.e. registerUserNotificationSettings.
However, when calling an UIAlertView or UIAlertController, UIApplicationWillResignActiveNotification and UIApplicationDidBecomeActiveNotification are not called when the focus changes from the app to the alert.
Question:
What is going on? What is the equivalent notifications triggered for
the application’s inactive and active state when UIAlertView or
UIAlertController are displayed and dismissed?
When your own code explicitly displays a UIAlertController (or UIAlertView), there is no notification. Your app already knows you are showing the alert because you just showed it.
Other system alerts, like the privacy alerts, are not shown by your app. They are shown by iOS. That is the difference. Your app isn't active because the OS has now active showing the alert. Your app becomes active again when the system alert is done.

remove/update notification from lock screen

I use UILocalNotification to show alerts.
I need to show two alerts 2 hours apart.
Right now, my code displays two different notifications on lock screen.
Assume, alerts/notifs were not touched by user during this period.
Is it possible for me to
Either remove the first shown notification from lock screen
Or update the first notification with the contents of second alert/notifcation.
To my understanding, you cannot modify a UILocalNotification once it has been scheduled.
To dismiss an already fired UILocalNotification, try calling the [[UIApplication sharedApplication] cancelAllLocalNotifications];
and then re-add those that were not yet fired.

App background - foreground states

Is there a way i could stop an app from going into background ? Or is there a way in which i could bring my app to foreground if it did go into background ?
I'm making a showcase app for a client and the app must always run on the iPad without interaction from the user.
You can force your app to stay active using following code
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
But if you press home button of device, the app will go in background. You cannot force to stop this.
In this case you can use UILocalNotification to bring the app to the foreground if the device is locked, a notification appears, and the user unlocks the device or if user tap on the notification.
You can also fire a local notification whenever user going to background in
applicationDidEnterBackground: method

Is it possible to change the display duration of a UILocalNotification?

I need to display a local notification and have it remain on screen longer than the default 4-5 seconds, preferably until the app itself removes it. I've seen other apps (e.g. Pandora) that manage to do this somehow (maybe a push notification?), but I can find no duration property on UILocalNotification or in UIApplication methods like presentLocalNotificationNow:, scheduleLocalNotification:, etc. Neither the documentation nor any of the tutorials I've found address the display time at all. Is this something that just can't be done with local notifications?
A couple of solutions here and I would not recommend either:
1- You can request from the user to go to settings > Notification Center > your app. And change the alert style from Banner (default) to Alerts. This will present the user an alert similar to the alert presented when the app is in the foreground. The user would have to dismiss the alert versus the banner style notification that just appears/disappears. Unless this is a corporate app and you have the users buy in, I would not go that route as this could annoy the user.
2- I tested the sound clip method and yes, if you present a notification with a clip < 30seconds; the notification will stay on the (top of the) screen until the sound clip is finished playing. Having said that, if the user taps any of the volume button (to reduce the sound for example), the notification is immediately dismissed even before its end! I think though that the purpose of the notification is a gentle reminder and, lasting more than the typical 4-5 seconds goes against the norm and, it might annoy the user (or the user might think something is stuck, phone froze, etc..). Here is the code anyway:
UILocalNotification *howLongCanANotificationLast = [[UILocalNotification alloc]init];
howLongCanANotificationLast.alertBody=#"I am a notification";
howLongCanANotificationLast.soundName=#"musicfilename.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:howLongCanANotificationLast];
Hope this helps.

Resources