Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone how to access the notification center programmatically

As per the Apple guide:

If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194

As far as I have known, it seems not be able to detect the notification when the application icon is tapped.

So I tried to retrieve the notification center programmatically but it also seems to be impossible.

Is it impossible to retrieve the notification center programmatically?

What I want to do is detecting whether the notifications received or not even when the application is in the background.

like image 650
zono Avatar asked Oct 07 '13 01:10

zono


People also ask

How do I view Notification Center on iPhone?

To see your notifications in Notification Center, do any of the following: On the Lock Screen: Swipe up from the middle of the screen. On other screens: Swipe down from the top center. Then you can scroll up to see older notifications, if there are any.

How do I access the Notification Center?

To view Notification Center on the Lock screen, swipe upward from the middle of the screen until it appears. (You can enable or disable Notification Center on the Lock screen in Settings > Touch ID & Passcode or Face ID & Passcode.)

What is UNUserNotificationCenterDelegate?

UNUserNotificationCenterDelegate. An interface for processing incoming notifications and responding to notification actions.


1 Answers

Is it impossible to retrieve the notification center programmatically?

No, it's not possible with any public API.

Your app is agnostic about the current state of the Notification Center, as they are two decoupled entities.

Anyway, as noted by AdamG, in iOS 7 you can implement

application:didReceiveRemoteNotification:fetchCompletionHandler:

which, according to the documentation, is called regardless of the state of your app (so even when it's not running or in background).

In order to use it, you have to support the remote-notification background mode. Here's how:

In Xcode 5 and later, you declare the background modes your app supports from the Capabilities tab of your project settings. Enabling the Background Modes option adds the UIBackgroundModes key to your app’s Info.plist file. Selecting one or more checkboxes adds the corresponding background mode values to that key.

Now, while you still cannot programmatically access to the Notification Center, you can track the notifications as they come.

An mocked implementation would go as follows:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // Hey we got a notification!
    // Now we have 30 seconds to do whatever we like...
    // ...and then we have to call the completion handler
    completionHandler(UIBackgroundFetchResultNoData);
}
like image 124
Gabriele Petronella Avatar answered Oct 08 '22 10:10

Gabriele Petronella