UILocalNotification
의 사용 방법에 대한 가이드를 읽었습니다. 그래서 시도해 보았고 첫 시도부터 성공하지 못했습니다. 응용 프로그램은를 실행 중일 때로컬 알림은 장치에서 작동하지 않지만 시뮬레이터에서 작동합니다
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
//if it is iOS 8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // if iOS 7
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
return YES;
}
및 알림을받을 수 :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"notification recieved %@",notification.alertBody);
// NSLog(@"notification userinfo %@",notification.userInfo);
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSLog(@"notification fired in foreground");
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Notification!"
message:notification.alertBody
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
} else if (state == UIApplicationStateInactive){
NSLog(@"notification fired in inactive");
} else if (state == UIApplicationStateBackground){
NSLog(@"notification fired in background");
}
}
을 그리고 모든 시뮬레이터에 너무 장치에 괜찮 - 응용 프로그램 인 경우 AppDelegate.m에 알림을 등록하려면 내가 사용 나는 예약 된 알림을받습니다. 그러나 응용 프로그램이 비활성 상태 일 때 응용 프로그램, 즉 실행되지 않을 때 알림을받을 필요가
문제는 내가 예를 들어 버튼을 눌러 내 알림을 shedule 경우, 내가 어떤없이 그것을 얻을 것이다 (집은 한 번 누름) 문제는 알림 센터에 표시되며 내 앱 아이콘에 배지가 추가됩니다. 하지만 내 AFNetworking
작업이 종료 될 때만 알림을 수신하려고합니다. 알림을 설정하려면 내가 방법에 따라 사용하십시오
-(void)getProgress{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *token = [defaults objectForKey:@"token"];
NSString *header = [NSString stringWithFormat:@"Bearer %@",token];
NSDictionary *params = @{@"lang": @"en",@"project":projId};
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:header forHTTPHeaderField:@"Authorization"];
[manager POST:@"http://example.com/api/get-progress" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([[responseObject objectForKey:@"result"]isEqualToString:@"success"]){
progCreate = [responseObject objectForKey:@"progress"];
if ([progCreate intValue]==100){
//shedule notification for immediately showing
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
localNotification.alertBody = [NSString stringWithFormat:@"Notification text!"];
localNotification.alertAction = @"Show";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[localNotification setSoundName:UILocalNotificationDefaultSoundName];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification]; the same result as above
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Error: %@", error);
}];
}
가장 큰 문제에만 iOS 버전과 시뮬레이터에서 작동 이 방법은 8.0하고 아이폰 OS 7.1.1와 아이폰 4에서 작동하지 않는다는 것입니다 iPhone 5c와 iOS 8.1.2는 iOS 7.1과 함께 시뮬레이터에서 작동하지 않습니다. iOS 7.1 이상의 실제 기기 및 시뮬레이터에서 백그라운드에서 알림을 수신하는 것으로 눈치 채지 못했지만 앱을 열면 알림을받을 수 있습니다 (AppDelegate.m
으로 설정 됨). 기꺼이 기다려주세요. 모든 제안 및 도움.
왜 화재 날짜가 즉시 설정되어 있습니까? 포 그라운드에서 항상 당신을 위해 불을 피우지 않겠습니까? 그것은 나의 상태를 만족시키기 때문에 미래 날짜 –
@ 스텐 브 리치에서 발사해야합니다. 'progCreate == 100' – vendettacore
@Bhavin 아니요, 서버 쪽에서 푸시 알림으로 해결했지만 로컬 알림에 여전히 – vendettacore