5

iOS에서 푸시 알림을 보내기 위해 FCM (Firebase Cloud Messaging)을 사용하고 있습니다.앱이 iOS 10의 백그라운드에있을 때 푸시 알림을 수신하지 못했습니다.

앱이 포 그라운드 상태에있을 때 알림을받을 수 있습니다. 그러나 앱이 백그라운드 상태에있을 때 알림을받지 못합니다. 응용 프로그램이 전경 상태가 될 때마다 알림 만 수신됩니다.

내 코드는 다음과 같습니다

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { 
// Print message ID. 
NSDictionary *userInfo = notification.request.content.userInfo; 
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); 

// Pring full message. 
NSLog(@"%@", userInfo); 

if([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) 
{ 
    NSLog(@"INACTIVE"); 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 
else if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) 
{ 
    NSLog(@"BACKGROUND"); 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 
else 
{ 
    NSLog(@"FOREGROUND"); 
    completionHandler(UNNotificationPresentationOptionAlert); 
}} 



- (void)applicationDidEnterBackground:(UIApplication *)application { 


} 

앱은 배경 상태에 있습니다

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 

- 전혀 호출되지 않습니다.

앱 기능의 백그라운드 모드에서 푸시 알림 및 원격 알림을 사용하도록 설정했습니다. 그러나 앱은 아직 알림을받지 못하고 있습니다.

일부 StackOverflow 관련 질문을 언급했지만이 문제를 해결할 수 없었습니다. iOS 버전 10에 추가 할 내용이 있습니까? 아니면 코드에 실수가 있습니까?

+0

백그라운드에서 iOS 10 또는 다른 모든 버전에만이 기능이 제공되지 않습니까? –

+0

@AL. iOS 용으로 만 수신하지 않습니다. – Himanth

+0

코딩 해주세요. 문제를 해결할 것입니다. – user3182143

답변

8

iOS 10의 경우 아래 2 가지 방법을 호출해야합니다. 포 그라운드 상태에 대한

배경 상태에 대한

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
{ 
    NSLog(@"Handle push from foreground"); 
    // custom code to handle push while app is in the foreground 
    NSLog(@"%@", notification.request.content.userInfo); 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 
{ 
    NSLog(@"Handle push from background or closed"); 
    // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background 
    NSLog(@"%@", response.notification.request.content.userInfo); 
    completionHandler(); 
} 

그 전에, 우리는 AppDelegate.h 파일에 UserNotifications 프레임 워크와 수입을 추가해야합니다

#import <UserNotifications/UserNotifications.h> 
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 
+0

나는 똑같은 문제에 직면하고 있습니다. 위 단계를 따라 갔지만 불행히도 여전히 작동하지 않습니다. 너 나 좀 도와 줄 수있어? –

+0

http://stackoverflow.com/questions/43694163/how-to-get-local-notification-when-app-is-running-in-objective-c/43705956#43705956 – user3182143

+0

완료를 호출 할 필요가 없습니다. 매니저? –