У меня проблема с отображением локальных уведомлений. Насколько я читал в других потоках, сначала нужно создать и запланировать локальное уведомление с приложением.Локальное уведомление отображается без didFinishLaunchingWithOptions:
Для отображения этого уведомления необходимо использовать делегаты didFinishLaunchingWithOptions: (если приложение находится в фоновом режиме) и didReceiveLocalNotification: (если приложение находится на переднем плане).
Теперь, даже если я не изменил метод didFinishLaunchingWithOptions: уведомление уже просматривается, когда мое приложение находится в фоновом режиме.
Это не будет большой проблемой, если didFinishLaunchingWithOptions: по крайней мере будет использоваться, когда я укажу его вообще. Но это не так.
Так что моя проблема в том, что даже если я не использовал метод didFinishLaunchingWithOptions: уведомление становится отображаемым. Когда пользователь нажимает на уведомление, приложение переходит на передний план и запускается метод didReceiveLocalNotification:, и уведомление снова отображается.
Что я изначально хотел сделать, это отменитьAllLocalNotifications при выполнении didFinishLaunchingWithOptions :, но так как он не выполняется, я как бы застрял здесь.
Хорошо, может быть обходной путь с применениемWillEnterForeground: но, честно говоря, я хотел бы понять, почему уведомление отображается даже без указания того, что в 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