我对显示本地通知有相当大的理解问题。 据我在其他线程中阅读,首先使用应用程序创建并安排本地通知。
要显示该通知,必须使用委托(delegate) didFinishLaunchingWithOptions:(如果应用程序处于后台操作)和 didReceiveLocalNotification:(如果应用程序处于前台)。
现在,即使我没有更改 didFinishLaunchingWithOptions: 方法,当我的应用程序位于后台时,通知已经被查看。
如果 didFinishLaunchingWithOptions: 至少在我指定它时会被使用,那就不是什么大问题了。但事实并非如此。
所以我的问题是,即使我没有使用 didFinishLaunchingWithOptions: 方法,通知也会显示。当用户点击通知时,应用程序会转到前台,并触发 didReceiveLocalNotification: 方法并再次显示通知。
我最初想做的是在执行 didFinishLaunchingWithOptions: 时取消AllLocalNotifications,但由于它没有被执行,所以我有点卡在这里。
好吧,applicationWillEnterForeground: 可能有一个解决方法,但说实话,我想了解,为什么即使没有在 didFinishLaunchingWithOptions: 中指定,通知也会显示。
非常感谢您的所有帮助!谢谢!!
//
// myNotificationsClass.m
//
#import "myNotificationsClass.h"
@implementation myNotificationsClass
//Sets up a Local Notification with Message, TimeFromNow, BadgeNumber and UserInfo
//no Class Instance for calling this method needed!!
+ (void)setupLocalNotificationsWithMessage: (NSString *) message andTimeFromNow: (NSTimeInterval) seconds andAlertAction: (NSString *) alertAction andBadgeNumber: (NSInteger) badgeNumber andUserInfo: (NSDictionary *) infoDict {
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// create date/time information
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//setup Appearence and Message
localNotification.alertBody = message; //@"Time to get up!";
localNotification.alertAction = alertAction;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = badgeNumber;
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
@end
//overwrites the viewWillAppear: Method from the primary Class to display a Test Notification
@implementation UIViewController (localNotification)
- (void)viewWillAppear:(BOOL)animated {
[myNotificationsClass setupLocalNotificationsWithMessage:@"First Test after 2 Seconds" andTimeFromNow:2 andAlertAction:@"GoTo iSnah" andBadgeNumber:7 andUserInfo:nil];
}
@end
//receive Local Notifications even if the App is in Foreground
//overwrites the primery method
@implementation UIResponder (localNotificationForeground)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]
message:notification.alertBody
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
//reset Badge
application.applicationIconBadgeNumber = 0;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Because I don't want the Notification to be displayed twice
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]
message:notification.alertBody
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
//reset Badge
application.applicationIconBadgeNumber = 0;
}
return YES;
}
@end
最佳答案
你们很接近。如果应用程序在后台运行,则用户响应通知将不会导致应用程序“启动”。 “启动”意味着应用程序根本没有运行(前台或后台)。因此,仅将您想要在应用程序启动或因用户响应本地通知而启动时执行的代码放入其中。
因此,您的 didReceiveLocalNotification 方法需要检查应用程序状态,以查看当用户响应本地通知时它是在前台还是在后台。
您可以执行以下操作来区分前台和后台之间的逻辑:
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
if ( application.applicationState == UIApplicationStateActive ){
NSLog(@"Was already in the foreground");
}
else{
NSLog(@"Was in the background");
}
}
不过,这是一个小问题。文档指出 didReceiveLocalNotification 方法在 application:didFinishLaunchingWithOptions: 之后调用(如果该方法已实现)。因此,如果您实现了这两种方法,则需要确保这两种方法的组合在“启动”情况下正常工作。
关于cocoa - 在没有 didFinishLaunchingWithOptions 的情况下显示本地通知 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970239/