Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I be notified of a banner notification in iOS?

I'm writing an iOS application and I'd like to pause my app's motion content when the operating system decides to show a Banner Notification like this one:

Banner Notification

Is there a system NSNotification that I can observer or a method that gets called which I can react to? I've triedapplicationWillResignActive, but that isn't called in this case.

like image 868
Matthew Bischoff Avatar asked Aug 12 '13 00:08

Matthew Bischoff


People also ask

Why am I not getting banner notifications on my iPhone?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.

What is the difference between banners and alerts on iPhone?

Banners are the pop-up notifications you see while you're using your phone. Below those options are two more: Sounds and Badges. Sounds are the audio tone alerts. Badges are the red circles with numbers that show up on app icons, for example, the one on your email app showing how many unread messages you have.


1 Answers

I took a stab at it this morning, and I'm inclined to say that there's no public API for this.

I tried using the code outlined here, and didn't catch any notifications. Then, I ran a bunch of "tests" to see if I could find anything.

To test, I created a pair of applications, one to schedule notifications (GitHub link), and one to try and "catch" them (GitHub link). In my Sender app, I can send N notifications every N seconds. I picked some arbitrarily high value and sent them.

In my catcher, I've tried looking at visibleRect values up and down the layer hierarchy. (The keyWindow lives in a layer, but it's superview, and super layer.delegate are both nil) I haven't checked constraints, but that shouldn't matter. I've looked at the application's window, it's nil superview, it's layer, it's subviews. The application's bounds aren't effected either. The app is sandboxed so well, that springboard and notification center don't exist in the app's world.

I started going down the path of accessing private frameworks, but decided it wasn't worth the effort.

I've opened Instruments and looked at the transparency levels of the views. (Is it possible to force all views in a hierarchy to be opaque, and then use that to see if the banner is blocking something? Perhaps it's not "blocking" if it's transparent?)

I've also attempted to take a screenshot, and check the colors in the top area of the screen, but that wouldn't work because you need to pass a view in to the context. Even if it would work, it wouldn't be particularly performant.

Another thought I've had would be to listen for push notifications on the push port, but I doubt that Apple would allow you to catch another app's notifications. As a developer, I wouldn't send private info in an alert, but it's still a concern.

The truth is that notification banners don't really cause your application to become inactive, so I'm not sure that this behavior is wrong. If it's a convenience, file a bug. How about requesting DeviceWillShowNotificationNotification?

like image 152
Moshe Avatar answered Nov 15 '22 08:11

Moshe