我对显示本地通知有一个很好的理解问题。就我在其他线程中阅读的内容而言,首先要使用应用程序创建和安排本地通知。
为了显示该通知,必须使用代表 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