0

이벤트 날짜가 가까워지면 알림 센터를 통해 사용자에게 알리미를 보내는 앱을 작성하고 있습니다. 그러나 날짜 선택 도구에서 날짜를 설정하고 앱을 종료하면 알림이 표시되지 않습니다. 프로비저닝 프로파일에서 이미 푸시 알림을 활성화했습니다. 내 objectForKey 영역 때문일 수 있습니다. 나는 ImportantDatesViewController_iPhone1 및 ImportantDatesViewController_iPad1이라는 2 개의 닙 (하나는 iPhone 용, 하나는 iPad 용)을 가지고 있습니다. objectForKey 영역을 "ImportantDatesViewController"대신 nib 이름으로 변경해야합니까? 그리고 .h 및 .m 파일 이름은 단순히 ImportantDatesViewController입니다. 미안하지만, 나는 아직도 이것에 대해 아주 새롭고 내가가는 것처럼 배우고있다. 여기 알림 센터를 다루는 내 프로젝트의 모든 코드입니다, 이것은 내가 내보기 컨트롤러에 넣어 것입니다 :iPhone iOS : 로컬 알림이 나타나지 않습니다.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
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:-60*60*60]; 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

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

localNotif.alertAction = nil; 

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

return YES; 

}

나는 또한 앱 내 didFinishLaunchingWithOptions 방법이 코드를 추가

아래쪽의 대표자, 그리고 트릭을 할 것이라고 생각 :

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

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

+0

그냥 보조 노트, 나는 내가 이전을 삭제하고 올바른 형식이 게시, 이전에 이것에 대해 게시하지만, 잘못을 기록했다. 의 – John

+0

가능한 중복 (http://stackoverflow.com/questions/8958898/iphone-local-notifications-wont-appear) – Abizern

답변

4

당신의 당신이 그것을 설정할 때 즉시 알림을 화재를 말하는하고 설정하여 지금은 전에 시간을 설정 (NOW) -60 * 60 * 60.The 알림이 이미 통과했다.

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif]; 

는 특정 시간을 설정하려면 : 지정된 값이 전무하거나 과거의 날짜입니다

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

경우, 통지는 즉시 전달된다. 사이드 노트로 timeZone과 Locale을 원하는 것으로 설정하십시오.

+1

정말 확인 결코 당신이 그냥 언급 할 때까지, 그러나 내 [아이폰 현지 알림이 표시되지 않습니다]를 거기에 나타납니다. 장치를 테스트 할 때 어떤 이유가 있는지 알 수 없습니다. –

+0

아, 한 가지 더 그렇다면 날짜 선택 도구에서 사용자가 설정 한 날짜보다 60 시간 전에 알림을 보내려는 올바른 공식은 -60 * 60 * 60입니다. – John

+1

NSDateComponents를 많이 사용하고 -60 시간을 설정하기 때문에 확신 할 수 없습니다. 그러나 당신의 솔루션은 견고합니다. 나는 100 % 확신 할 수 없습니다. 때로는 같은 일을하는 한 가지 이상의 방법이 있습니다. –

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

    if (localNotif == nil) return; 
    NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:900] ; //15 min 

      localNotif.fireDate = fireTime; 
      localNotif.alertBody = @"Parking your CAR 15 Minutes reached.."; 
      // localNotif.repeatInterval=kCFCalendarUnitMinute; 
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
1

다음은 내 프로젝트에서 사용 된 LocalNotification의 샘플 코드입니다.

AppDelegate에 파일이 코드 블록 :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
     // Override point for customization after application launch. 
     return YES; 
    } 

    // This code block is invoked when application is in foreground (active-mode) 
    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 

     UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"This local notification" 
     delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 

     [notificationAlert show]; 
     // NSLog(@"didReceiveLocalNotification"); 
    } 

임의의 ViewController의하는 .m 파일이 코드 블록 :

-(IBAction)startLocalNotification { // Bind this method to UIButton action 
    NSLog(@"startLocalNotification"); 

    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7]; 
    notification.alertBody = @"This is local notification!"; 
    notification.timeZone = [NSTimeZone defaultTimeZone]; 
    notification.soundName = UILocalNotificationDefaultSoundName; 
    notification.applicationIconBadgeNumber = 10; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
} 

상기 코드 표시 7초 기간 후 AlertView 누르면 "startLocalNotification"을 바인드하는 on 버튼 응용 프로그램이 백그라운드에 있으면 BadgeNumber가 10이고 기본 알림 소리가 표시됩니다.

관련 문제