2016-12-27 26 views
0

我已配置远程通知的操作,当它到达我的iOs应用程序。我需要两种不同的操作,无论一个密钥是否通过有效负载。该代码似乎被执行,但该应用程序无法打开,也没有Safari网址。这里是我的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. 当应用处于活动状态和通知来
  2. 当应用程序在后台或不活动或终止通知和用户点击它