2017-06-30 4 views
1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    [UNUserNotificationCenter currentNotificationCenter].delegate = self; 
    UNAuthorizationOptions authOptions = 
    UNAuthorizationOptionAlert 
    | UNAuthorizationOptionSound 
    | UNAuthorizationOptionBadge; 
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { 


    }]; 


    [FIRMessaging messaging].remoteMessageDelegate = self; 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 

    [FIRApp configure]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) 
               name:kFIRInstanceIDTokenRefreshNotification object:nil]; 


    return YES; 
} 

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

} 

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


    [[FIRMessaging messaging] disconnect]; 
    NSLog(@"Disconnected from FCM"); 
} 

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


} 

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


    [self connectToFcm]; 

} 

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

} 




- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 



    // Print message ID. 
    NSLog(@"message 1"); 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 

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

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 


    // Print message ID. 
    NSLog(@"message 2"); 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 


    NSLog(@"%@", userInfo); 

    completionHandler(UIBackgroundFetchResultNewData); 
} 



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

    NSLog(@"message 3"); 
    NSDictionary *userInfo = notification.request.content.userInfo; 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 


    NSLog(@"full data : %@", userInfo); 

    completionHandler(UNNotificationPresentationOptionAlert); 


} 



- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
didReceiveNotificationResponse:(UNNotificationResponse *)response 
     withCompletionHandler:(void (^)())completionHandler { 
    NSDictionary *userInfo = response.notification.request.content.userInfo; 
    NSLog(@"message 1"); 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 

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

    completionHandler(); 
} 

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
    // Print full message 
    NSLog(@"applicationReceivedRemoteMessage : %@", remoteMessage.appData); 

} 


- (void)tokenRefreshNotification:(NSNotification *)notification { 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *refreshedToken = [[FIRInstanceID instanceID] token]; 
    NSLog(@"InstanceID token: %@", refreshedToken); 

    if(!(refreshedToken== nil)){ 
     [defaults setValue:refreshedToken forKey:@"FcmToken"]; 
    } 


    [self connectToFcm]; 


} 

- (void)connectToFcm { 

    if (![[FIRInstanceID instanceID] token]) { 
     return; 
    } 


    [[FIRMessaging messaging] disconnect]; 

    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { 
     if (error != nil) { 
      NSLog(@"Unable to connect to FCM. %@", error); 
     } else { 
      NSLog(@"Connected to FCM."); 
     } 
    }]; 
} 

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 
    NSLog(@"Unable to register for remote notifications: %@", error); 
} 


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSLog(@"APNs token retrieved: %@", deviceToken); 
} 

enter code here 

アプリがバックグラウンドモードのときは、メッセージは表示されません。アプリケーションがフォアグラウンドモードのメッセージを表示しているときに、applicationReceivedRemoteMessage関数に表示されます。バックグラウンドモードのときにメッセージが来たら、私はいくつかのコードを実行する必要があります。誰も私に通知をバックグラウンドモードで動作させるためのソリューションを私に与えることができます。Firebaseメッセージングは​​、フォアグラウンドでのみ動作します

答えて

1

FIRMessaging接続は、バックグラウンドでライブ接続できません。接続を閉じるには、 が賢明です。同じのため

こちらをご覧ください参照:チャットアプリケーションについてdisconnect()

FirebaseMessaging Framework Reference

シンプルなロジック:


一般ソケット接続、アプリケーションのチャットは、他のノード(機器)と接続しますソケット接続を使用して、ノード間でリアルタイム情報を送信します。 アプリがバックグラウンドになると、ソケット接続が切断されます。

FirebaseMessagingは同じロジックで動作するため、バックグラウンドでは動作しません。

バックグラウンドモードでのメッセージ送信を処理するには、のPushNotificationの出力を使用します。

コードにもマークを付けてください:アプリケーションがバックグラウンドになると、FIRMessagingを切断しています。これは、FIRMessagingガイドラインにも記載されているため、これを実行しました。問題の代替ソリューションとして

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

    [[FIRMessaging messaging] disconnect]; 
    NSLog(@"Disconnected from FCM"); 
} 

:あなたはWhatappやFacebook Messagnerアプリを解析している可能性があります。彼らは、アプリがバックグラウンドに入ると、プッシュ通知を使用してメッセージをユーザーに警告します。同じことをする必要があります。

+0

あなたは解決策として何を提案しますか? – user3045354

+0

あなたはWhatappまたはFacebook Messagnerアプリを分析したかもしれません。彼らは、アプリがバックグラウンドに入ったときに、ユーザーにメッセージを知らせるためにプッシュ通知を使用します。 – Krunal

+0

ここで私の問題はバックグラウンドで、どちらもアプリにアラートが届かないときです。 – user3045354

関連する問題