【发布时间】:2016-03-27 10:03:26
【问题描述】:
我有一个使用本地通知的 iOS 应用程序。当应用程序在后台运行或处于活动状态时,我使用 application didReceiveLocalNotification 方法,效果很好。但是当应用关闭时(不在后台运行),我使用application didFinishLaunchingWithOptions 方法来控制处理通知时会发生什么。
但是,我遇到的问题是didFinishLaunchingWithOptions 方法中的通知始终是(null)。
这是我的代码:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Register the app for local notifcations.
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
// Setup the local notification check.
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
//UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:notification delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//[alertView show];
// Check if a notifcation has been received.
if (notification) {
// Run the notifcation.
[self.myViewController run_notifcation:[notification.userInfo valueForKey:@"notification_id"]];
}
// Ensure the notifcation badge number is hidden.
application.applicationIconBadgeNumber = 0;
return YES;
}
我在这里遗漏了什么吗?我已经先询问了用户的权限,并拥有正确的代码来获取通知对象。我的应用仅适用于 iOS 9 及更高版本。
感谢您的宝贵时间,丹。
【问题讨论】:
-
你是怎么发现通知总是
null的?您是否在调试模式下从 Xcode 运行应用程序?默认情况下没有通知开始 -
@Azat 我尝试在 uialertview 中显示通知对象。
-
当应用程序没有在后台运行并且用户点击通知时,应用程序将被启动并且通知将具有价值。确保用户点击通知
-
请确保用户点击通知,同时打印
launchOptions字典以查看是否有任何值 -
@user1046037 可能是因为我的代码正在调用来自
self.myViewController的方法,而此时应用程序尚未加载?
标签: ios objective-c uilocalnotification appdelegate uiapplicationdelegate