2012-06-10 2 views
0

로컬 알림을 표시하는 데 상당한 문제가 있습니다. 다른 스레드에서 읽는 한 먼저 응용 프로그램과 함께 로컬 알림을 만들고 예약합니다.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 

답변

1

매우 가까이 있습니다. 앱이 백그라운드에서 실행중인 경우 사용자가 알림에 응답해도 앱이 실행되지 않습니다. "시작"은 앱이 전혀 실행되지 않고 있음을 나타냅니다 (전경 또는 배경). 따라서 사용자가 로컬 알림에 응답하여 앱을 시작하거나 시작할 때 실행하려는 코드 만 삽입하십시오.

그래서 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 : (해당 메소드가 구현 된 경우) 다음에 호출된다는 내용이 나와 있습니다. 두 가지 방법을 모두 구현 한 경우 2의 조합이 "시작"상황에서 올바르게 작동하는지 확인해야합니다.

관련 문제