didReceiveRemoteNotification没有被调用

时间:2018-09-04 10:36:54

标签: objective-c salesforce-marketing-cloud

我正在使用Salesforce Marketing Cloud iOS SDK(v4.9.7)。我能够接收推送通知并能够调用'didReceiveNotificationResponse'。但是我无法调用'didReceiveRemoteNotification'。可能是什么问题?

My Capabilities here

My info.plist here

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

  [[ETPush pushManager] handleNotification:userInfo forApplicationState:application.applicationState];




  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to say hello?"
                                                  message:@"More info..."
                                                 delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"Say Hello",nil];
  [alert show];
}

我的AppDelegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
    [self application:application shouldInitETSDKWithOptions:launchOptions];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
      UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
      [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }
    else
    {
      [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
       (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

    [[ETPush pushManager] setCloudPageWithAlertDelegate:self];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;

    return YES;
  }

1 个答案:

答案 0 :(得分:0)

文档:

用户通知框架Here

从iOS 11起,方法didReceiveRemoteNotificationdeprecated

展望未来,使用UserNotifications框架方法:userNotificationCenter:willPresentNotification:withCompletionHandler:

可能看起来像这样

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

请注意,iOS目标below 11仍需要实现didReceiveRemoteNotification方法。

// in app delegate file at top 
#if canImport(UserNotifications)
import UserNotifications
#endif
class AppDelegate { // ... abbreviated  }

@available (iOS 11, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
   func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      // your code to handle 
   }
}