2012-01-22 3 views
1

가능한 복제를 나타나지 않습니다 :
UILocalNotification isn't working at all아이폰 현지 알림

나는 이벤트 날짜가 접근 할 때 알림 센터를 통해 사용자에게 경고를 보내는 응용 프로그램을 쓰고 있어요 . 그러나 날짜 선택 도구에서 날짜를 설정하고 앱을 종료하면 알림이 표시되지 않습니다. 프로비저닝 프로파일에서 이미 푸시 알림을 활성화했습니다. 이것은,이 날짜 선택을 다루는 내보기 컨트롤러 파일에 모든 코드를 알림 센터를 다루는 내 프로젝트의 모든 코드입니다 :

- (IBAction)dateChanged:(id)sender 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSDate *selectedDate = [self.datePicker date]; 

    [defaults setObject:selectedDate forKey:@"ImportantDatesViewController.selectedDate"]; 
    [defaults synchronize]; 

} 

- (void)viewDidLoad { 
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] 
          objectForKey:@"ImportantDatesViewController.selectedDate"]; 
    if (storedDate == nil) { 
     storedDate = [NSDate date]; 
    } 

    [self.datePicker setDate:storedDate animated:NO]; 

} 

그리고이 지역의 통지를 처리 ​​내 앱 위임의 모든 것 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
.... 

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    UIRemoteNotificationTypeBadge | 
    UIRemoteNotificationTypeAlert | 
    UIRemoteNotificationTypeSound]; 

    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"]; 

    NSDate *eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"ImportantDatesViewController.selectedDate"]; 



    localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60]; 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 


    localNotif.alertBody = @"Event in three days!"; 

    localNotif.alertAction = nil; 

    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 0; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];  

    return YES; 

} 

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSString* pushToken = [[[[deviceToken description] 
          stringByReplacingOccurrencesOfString:@"<"withString:@""] 
          stringByReplacingOccurrencesOfString:@">" withString:@""] 
          stringByReplacingOccurrencesOfString: @" " withString: @""]; 

    NSLog(@"%@", pushToken); 

} 

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { 

    NSLog(@"error: %@", error); 
} 

어떤 도움을 주셔서 감사합니다.

+1

'NSUserDefaults'에서 검색 할 때 키의 펜촉 이름을 사용한다고 말하면서 다른 질문에 혼란 스러울 수 있습니다. 키가 고유한지 확인하는 것은 개인적인 취향의 문제입니다. 문제의 진실은 당신이 그것을 저장하기 위해 당신이 무엇을 사용하든 그것을 사용해야한다는 것입니다. 'eventDate'를 저장하고있는 곳을 찾고 같은 키를 사용하십시오. –

+0

Mark에게 감사드립니다. eventDate를 어디에 저장할지 결정하려고하는데 문제를 찾는 중입니다. 내 펜촉에있는 피커에있을 것인가? – John

+1

로컬 알림을 시작하려는이 날짜를 어떤보기 컨트롤러에서 사용자에게 묻는 중입니까? 이 클래스에서는 어딘가에 'NSUserDefaults'에 날짜를 저장하고 있습니다. 동일한 키를 사용하십시오. 이제 왜 내가 키를 상수로 저장한다고 언급했는지 알 수 있습니다. :) –

답변

2

다음 코드는 로컬 알림에 사용됩니다.

-(IBAction)buttonPressed:(UIButton *)button 
{ 
    UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 

    if (!localNotification) 
     return; 

    // Current date 
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time 
    NSDate *dateToFire = [date dateByAddingTimeInterval:20]; 

    // Set the fire date/time 
    [localNotification setFireDate:dateToFire]; 
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]]; 

    // Create a payload to go along with the notification 
    NSArray *array = [NSArray arrayWithObjects:@"Value 1", @"Value 2", nil]; 
    NSDictionary *data = [NSDictionary dictionaryWithObject:array forKey:@"payload"]; 
    [localNotification setUserInfo:data]; 

    if (button == buttonAlert || button == buttonAll) 
    { 
     // Setup alert notification 
     [localNotification setAlertBody:@"Incoming Local Notification" ]; 
     [localNotification setAlertAction:@"Open App"]; 
     [localNotification setHasAction:YES]; 
    } 

    if (button == buttonBadge || button == buttonAll) 
    { 
     // Set badge notification, increment current badge value 
     [localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1]; 
    } 

    if (button == buttonSound || button == buttonAll) 
    { 
     // Setup sound notification 
     [localNotification setSoundName:UILocalNotificationDefaultSoundName]; 
    } 

    // Schedule the notification 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
}