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.
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.
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.)
UNUserNotificationCenterDelegate. An interface for processing incoming notifications and responding to notification actions.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With