iPhone 如何以编程方式访问通知中心 [英] iPhone how to access the notification center programmatically

查看:66
本文介绍了iPhone 如何以编程方式访问通知中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Apple 指南:

As per the Apple guide:

如果在运行 iOS 的设备上点击应用程序图标,应用程序会调用相同的方法,但不提供有关通知的信息.如果在运行

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.

推荐答案

是否无法以编程方式检索通知中心?

Is it impossible to retrieve the notification center programmatically?

不,任何公共 API 都不可能.

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.

无论如何,正如 AdamG 所指出的,在 iOS 7 中你可以实现

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).

要使用它,您必须支持remote-notification 后台模式.方法如下:

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

在 Xcode 5 及更高版本中,您可以从项目设置的 Capabilities 选项卡中声明您的应用程序支持的后台模式.启用后台模式选项会将 UIBackgroundModes 键添加到应用程序的 Info.plist 文件中.选择一个或多个复选框将相应的背景模式值添加到该键.

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.

模拟实现如下:

- (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);
}

这篇关于iPhone 如何以编程方式访问通知中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆