로컬 알림을 표시하는 데 상당한 문제가 있습니다. 다른 스레드에서 읽는 한 먼저 응용 프로그램과 함께 로컬 알림을 만들고 예약합니다.didFinishLaunchingWithOptions없이 로컬 알림이 표시됩니다.
알림을 표시하려면 대리인 didFinishLaunchingWithOptions :(앱이 백그라운드 작업 인 경우) 및 didReceiveLocalNotification : (앱이 포 그라운드 인 경우)을 사용해야합니다.
이제는 didFinishLaunchingWithOptions : 메소드를 변경하지 않았지만 내 앱이 백그라운드에있을 때 알림이 이미 표시되고 있습니다.
didFinishLaunchingWithOptions :가 적어도 지정 될 때 문제가되지는 않습니다. 그러나 그렇지 않습니다.
그래서 didFinishLaunchingWithOptions : 메서드를 사용하지 않았더라도 알림이 표시됩니다. 사용자가 알림을 클릭하면 앱이 포어 그라운드로 이동하고 didReceiveLocalNotification : 메소드가 실행되고 알림이 다시 표시됩니다.
원래 didFinishLaunchingWithOptions :의 실행시 cancelAllLocalNotifications를 사용하고 싶었지만 실행되지 않았기 때문에 여기에 막 달라 붙어 있습니다.
좋아, 거기에 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