2016-12-27 2 views
0

iOsアプリケーションに到着したときにリモート通知のアクションを設定しました。私は、ペイロードに1つのキーが渡されたかどうか、2つの異なるアクションが必要です。コードは実行されているようだが、アプリは開かず、SafariのURLも表示されない。ここに私のAppDelegate.mがあります:リモート通知アクションが何もしないのはなぜですか?

NSString *const NotificationCategoryOpenView = @"openView"; 
NSString *const NotificationActionOpenView = @"View"; 

- (void)registerForNotification { 
    UIApplication *application = [UIApplication sharedApplication]; 

    // iOs 8 or greater: 

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 

     UIMutableUserNotificationAction *open; 
     open = [[UIMutableUserNotificationAction alloc] init]; 
     [open setActivationMode:UIUserNotificationActivationModeBackground]; 
     [open setTitle:NSLocalizedString(@"View", nil)]; 
     [open setIdentifier:NotificationActionOpenView]; 
     [open setDestructive:NO]; 
     [open setAuthenticationRequired:NO]; 

     UIMutableUserNotificationCategory *actionCategory; 
     actionCategory = [[UIMutableUserNotificationCategory alloc] init]; 
     [actionCategory setIdentifier:NotificationCategoryOpenView]; 
     [actionCategory setActions:@[open] 
         forContext:UIUserNotificationActionContextDefault]; 

     NSSet *categories = [NSSet setWithObject:actionCategory]; 
     UIUserNotificationType types = (UIUserNotificationTypeAlert| 
             UIUserNotificationTypeSound| 
             UIUserNotificationTypeBadge); 

     UIUserNotificationSettings *settings; 
     settings = [UIUserNotificationSettings settingsForTypes:types 
                categories:categories]; 

     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
    } else if ([application respondsToSelector:@selector(registerForRemoteNotificationTypes:)]) { 
     // iOs 7 or lesser: 

     UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; 
     [application registerForRemoteNotificationTypes:myTypes]; 
    } 

} 

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler { 

    if ([identifier isEqualToString:NotificationActionOpenView]) { 

     NSDictionary *aps = userInfo[@"aps"]; 

     if ([[aps allKeys] containsObject:@"viewToOpen"]){ 
      NSString *webString = [[NSString alloc] initWithFormat:@"%@", aps[@"viewToOpen"]]; 
      NSURL *webURL = [[NSURL alloc] initWithString:webString]; 

// This line doesn't work: 
      [[UIApplication sharedApplication] openURL:webURL]; 

     } else { 
// These two lines doesn't work: 
      UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 
      [navigationController popToRootViewControllerAnimated:NO]; 
     } 
    } 

    completionHandler(); 

} 

ありがとう!

+0

"' UserNotifications Frameworkの使用 - [UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandlerを:] '" 真のiOSの10.0 – Arasuvel

+0

のために、別のエラーが、私はiOSの10、感謝の世話をしていなかったでした。) – BigKangu

答えて

0

実装

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler 

そして

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

方法。 通知アクティベーションモードはUIUserNotificationActivationModeBackgroundに設定されています。これは、アプリを有効にしてバックグラウンドに配置します。

[open setActivationMode: UIUserNotificationActivationModeForeground]; 

チェック気象webUrl:アプリがフォアグラウンドにすでにある場合、それはあなたがフォアグラウンドでアプリケーションにしたい場合は/この起動オプションUIUserNotificationActivationModeForeground

[open setActivationMode:UIUserNotificationActivationModeBackground]; 

使用を使用し、その後起動foreground.Soのまま正常であり、通常の流れでは期待どおりに動作しています。

Xcode 8では、UserNotificationフレームワークを使用してプッシュ通知を移行する必要があります。

+0

主な問題はActivationModeだったので、あなたに感謝多く! – BigKangu

1

あなたのコードは、このメソッドが呼び出されます

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

    if(userInfo) { 
     UIApplicationState state = [[UIApplication sharedApplication] applicationState]; 
     NSUInteger notificationType = [[userInfo valueForKey:API_NOTIFICATION_TYPE] integerValue]; 
     if (state == UIApplicationStateActive) { 

      // Write code which should work when notification comes and app is active 
      } 
      else { 
       // Write code which should work when notification comes and app is in background or inactive or terminated 
      } 
     } 
    } 
} 

この方法にする必要がありますのみ

  1. アプリはACTIVEであると通知が来るとき
  2. たときにアプリがバックグラウンドまたは非アクティブまたは終了しており、通知が来て、ユーザがそれをタップする
関連する問題