2017-03-05 32 views
0

私のアプリはFirebaseからの通知を受け取ります。通知を受信すると、アプリケーションはローカル通知を表示する必要があるかどうかを判断します。どうすればいいの?このコードを試しましたが、通知は表示されません。通知を受け取ったときにiOS 7-10でローカル通知を表示するにはどうすればよいですか?

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Register for remote notifications 
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { 
    // iOS 7.1 or earlier. Disable the deprecation warnings. 
    #pragma clang diagnostic push 
    #pragma clang diagnostic ignored "-Wdeprecated-declarations" 
    UIRemoteNotificationType allNotificationTypes = 
     (UIRemoteNotificationTypeSound | 
     UIRemoteNotificationTypeAlert | 
     UIRemoteNotificationTypeBadge); 
    [application registerForRemoteNotificationTypes:allNotificationTypes]; 
    #pragma clang diagnostic pop 
    } else { 
    // iOS 8 or later 
    // [START register_for_notifications] 
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { 
     UIUserNotificationType allNotificationTypes = 
     (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); 
     UIUserNotificationSettings *settings = 
     [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    } else { 
     // iOS 10 or later 
     #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
     UNAuthorizationOptions authOptions = 
      UNAuthorizationOptionAlert 
      | UNAuthorizationOptionSound 
      | UNAuthorizationOptionBadge; 
     [[UNUserNotificationCenter currentNotificationCenter] 
      requestAuthorizationWithOptions:authOptions 
      completionHandler:^(BOOL granted, NSError * _Nullable error) { 
       #pragma unused(granted) 
       #pragma unused(error) 
      } 
     ]; 

     // For iOS 10 display notification (sent via APNS) 
     [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; 
     // For iOS 10 data message (sent via FCM) 
     [[FIRMessaging messaging] setRemoteMessageDelegate:self]; 
     #endif 
    } 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    // [END register_for_notifications] 
    } 

    // [START configure_firebase] 
    [FIRApp configure]; 
    // [END configure_firebase] 
    // Add observer for InstanceID token refresh callback. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) 
               name:kFIRInstanceIDTokenRefreshNotification object:nil]; 
    return YES; 
} 


-(void)dispathNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo { 
    NSLog(@"Received notification"); 
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { 
     if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) { 
      UNMutableNotificationContent *content = [UNMutableNotificationContent new]; 
      content.title = @"new notification"; 
      content.body = @"show content"; 
      content.sound = [UNNotificationSound defaultSound]; 
      UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:false]; 
      NSString *identifier = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]]; 
      UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; 
      [center addNotificationRequest:request withCompletionHandler:nil]; 
     } 
    }]; 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  
    [self dispathNotification:application userInfo:userInfo]; 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
    [self dispathNotification:application userInfo:userInfo]; 
    completionHandler(UIBackgroundFetchResultNewData); 
} 

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
    NSDictionary *userInfo = [remoteMessage appData]; 
    [self dispathNotification:[UIApplication sharedApplication] userInfo:userInfo]; 
} 
+0

あなたは何を意味するのですか?デバッグメソッドdispatchNotficationが呼び出され、すべてのステップが実行されたときに通知が受信されますが、中枢のaddNotificationRequestの後に何も起こらず、何も表示されません。 – mabg

答えて

0

ローカルまたはリモート通知を処理するには、2つの異なる方法を実装する必要があります。下を見てください。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ 
     UIApplicationState state = [application applicationState]; 
     if(state == UIApplicationStateActive){ 

      NSString *message = @""; 
      if([message isEqualToString:@""] || message == nil) 
       message = @"local notify"; 
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"local message" 
                   message:message 
                   delegate:self cancelButtonTitle:[[ContentKeeper getKeeper] getResourceForKey:@"OKButtonText"].Value 
                 otherButtonTitles:nil]; 
       [alert show]; 
     } 
    } 



- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    UIApplicationState state = [application applicationState]; 
    if (true) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                 message:notification.alertBody 
                 delegate:self cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    // Set icon badge number to zero 
    application.applicationIconBadgeNumber = 0; 
} 
+0

アラートは表示されますが、通知センターに通知する方法はありますか? – mabg

+0

あなたがアプリにいるとき、プッシュ通知が上から上がらなかった。あなたが望むならば、あなたはカスタムポップアップを開発し、アラートを変更するよりもそれを行うことができます。 –

0

見つけました。このコードを配置する場合

、通知を示しています

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ 
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 
} 
関連する問題