iphone – UILocalNotification的警报动作代码
发布时间:2020-12-14 19:50:34 所属栏目:百科 来源:网络整理
导读:UILocalNotification *notif = [[cls alloc] init];notif.fireDate = [self.datePicker date];notif.timeZone = [NSTimeZone defaultTimeZone];notif.alertBody = @"Did you forget something?";notif.alertAction = @"Show me"; 如果用户点击“showme”应用
UILocalNotification *notif = [[cls alloc] init]; notif.fireDate = [self.datePicker date]; notif.timeZone = [NSTimeZone defaultTimeZone]; notif.alertBody = @"Did you forget something?"; notif.alertAction = @"Show me"; 如果用户点击“showme”应用程序应该打开,他应该得到警报. 解决方法
根据应用程序在通知发生时的状态,您将在两个地方收到关于UILocalNotification的通知.
1.在应用程序中:didFinishLaunchingWithOptions:方法,如果应用程序既不运行也不在后台运行. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { // Show Alert Here } ... } 2.在应用程序中:didReceiveLocalNotification:方法,如果应用程序运行或在后台运行.在应用程序已经运行时显示警报几乎没用.因此,只有当应用程序在通知触发时处于背景状态时,才需要显示警报.要知道应用程序是否从后台使用applicationWillEnterForeground:方法. - (void)applicationWillEnterForeground:(UIApplication *)application { isAppResumingFromBackground = YES; } 使用此功能,只有当应用程序从后台恢复时,才能在didReceiveLocalNotification:方法中显示警报. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if (isAppResumingFromBackground) { // Show Alert Here } } 如果您希望在通知被触发的同时显示警报视图,您可以直接忽略if条件,无论应用程序的状态如何. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |