Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show an alert when iOS app is in the background

Tags:

I want to show an alert view when my iOS app is in the background (and it's using location).

For example, the Uber Partner (Driver) app shows an alert and plays a sound even when:

  • I have turned off notifications!
  • My iPhone is in Silent mode!

I am aware of the local notifications approach and it doesn't work if the user turns off/ changes the Notifications in Settings. I am looking for something different.

enter image description here
Actions performed to reach the above state:

  • Go online on Uber Partner App (you are the driver!)
  • Disable Notifications for the app in Settings
  • Move the app to background and wait for a Ride Request
  • After some time, a ride Request is popped up as an Alert view and a sound is played in the background

Of course, silent remote notifications can be tapped in by the app using the didReceiveRemoteNotification: fetchCompletionHandler: API even if the user disables Notifications in Settings. But, how the alert is popped up, that's what I am trying to find out.

like image 690
K.K Avatar asked Apr 16 '16 21:04

K.K


People also ask

How do I know if apps are running in the background iOS?

Go to Settings>General>Background App Refresh and you can see what other apps are allowed to update data in the background. iOS dynamically manages memory without any user intervention. The only apps that are really running in the background are music or navigation apps.

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

What is an iPhone banner alert?

Banner notifications on your iPhone are alerts that drop down from the top of your screen whenever you receive a notification from an app. You can enable or disable Banner notifications on an app-by-app basis through your iPhone's Settings app.

How do I turn on iOS app notifications?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered — immediately or in the scheduled notification summary.


1 Answers

I would imagine that Uber has some special permissions or uses some private API that allow them to achieve this behavior without using local notifications. While I don't know how Uber implemented this in their partner app, I can talk a little bit about how alerts work on the home screen.

SpringBoard is the singleton class that manages the SpringBoard application (SpringBoard.app), the application launcher for the iPhone. SpringBoard doesn't use the standard UIAlertView/UIAlertController classes, since they don't participate in the SpringBoard-wide alert system. iOS 5 introduced SBAlertItem the which is used to display UIAlertViews on SpringBoard (Battery Notification Alerts, Sim Unlock Alert, etc.). Apple uses SBAlertItem for their lock and home screen alerts, I'll be working on the assumption that Uber is using an SBAlertItem for this answer.

SBAlertItem has a protected ivar UIAlertView *_alertSheet. Assuming this acts as a normal UIAlertView, you should be able to change the properties on this alert to fit your needs. I would also read through saurik's Cydia Substrate project, specifically MobileSafety.mm to see some use cases. I've also found noweibogoodsleep which provides an example of using SBAlertItem on the SpringBoard.

I've also found SBUserNotificationAlert, a subclass of SBAlertItem. This appears to have more methods to facilitate alert customization that may fit your needs better than the standard SBAlertItem.

I realize hooking into private APIs is probably not what you were expecting when asking this question. Since I don't know how Uber works, I can only provide an answer from my personal experience working with the runtime and jailbroken devices.

like image 155
JAL Avatar answered Oct 20 '22 03:10

JAL