【发布时间】:2012-09-05 07:22:23
【问题描述】:
我一直在尝试在我的应用中处理接收通知,但它并没有真正奏效。
当我使用didReceiveLocalNotification:(UILocalNotification *)notification 时。我可以接收和使用用于进入应用程序的通知,没有任何问题
但是,只有当应用程序已经运行(活动、非活动、后台和可能暂停,但我还没有尝试过)时才会触发此函数。
现在,有这个函数didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,您可以在其中使用[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey],它会返回一个UILocalNotification。
但是,当您从 not-running 状态启动应用程序时,不会触发此事件。然后 LocalNotification 会打开应用程序,但我无法以任何方式使用它。
现在,我的问题是:我怎样才能让它工作,以便我可以在启动应用程序时接收和处理通知,从通知,当应用程序处于非运行状态时?是也许我在这里做错了什么?
这是我的应用程序中的一些示例代码:
首先,didFinishLaunchingWithOptions 函数,不幸的是它不起作用。函数[sharedLocalNotificationsInstance processNotification:notification] 永远不会启动...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
[sharedLocalNotificationsInstance checkNotifications];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if ( notification != nil ) {
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
application.applicationIconBadgeNumber = 0;
}
return YES;
}
还有第二段代码:didReceiveLocalNotification 函数,完美运行:我收到通知,[sharedLocalNotificationsInstance processNotification:notification] 运行完美。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// Used when the application launches from a notification
LocalNotificationsController *sharedLocalNotificationsInstance = [LocalNotificationsController sharedLocalNotificationsInstance];
// Process the received notification
[sharedLocalNotificationsInstance processNotification:notification];
}
【问题讨论】:
-
嗨拉尔斯克。我有同样的问题。当应用程序处于前台或后台时,我可以收到通知,但是当应用程序关闭时会发生有趣的事情。我点击通知,应用程序打开,在屏幕上停留 1 秒钟然后屏幕变黑。我在后台看到应用程序,但没有收到通知的迹象。我真的不确定 UILocalNotification* notification = (UILocalNotification*)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];工作与否。您是否找到了可靠的解决方案?
标签: objective-c ios uilocalnotification uiapplication