【问题标题】:How do I extend the UNUserNotificationCenterDelegate protocol in AppDelegate?如何在 AppDelegate 中扩展 UNUserNotificationCenterDelegate 协议?
【发布时间】:2021-03-29 09:35:52
【问题描述】:

我正在开发一个 React Native 应用程序,它使用两个不同的库来处理本地通知 (react-native-community/push-notification-ios) 和远程推送通知 (Leanplum)。 Leanplum SDK 使用 swizzling。不幸的是,即使我禁用了 swizzling,用于显示前台通知的推荐设置也会阻止正确处理远程通知。

我收到了来自远程通知 SDK 支持团队的反馈

您在本地层面的实施干扰了 Leanplum swizzle。您是否能够实现用户通知委托协议,在应用程序委托类中进行扩展?在 iOS 实现中,这允许两个逻辑(LP 和本地)自动且平滑。我们推荐的实现是让 AppDelegate 扩展 UNUserNotificationCenterDelegate 协议,特别是如果您想保留本地推送实现

虽然我正在尽我所能尽快学习 Objective-C,但我不是原生 iOS 开发人员,我不知道如何遵循这个建议。我已经阅读了很多关于设置推送通知的问题,但他们都推荐了以下设置,并且我无法找到任何处理这种前台通知处理干扰另一个库的情况。

如何扩展 AppDelegate 中的UNUserNotificationCenterDelegate 协议以在不干扰远程通知库的情况下处理前台通知?

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  [...]
    
  // Define UNUserNotificationCenter
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self; // <-- interferes with the remote notification SDK
  
  [RNSplashScreen show];
  return YES;
}

  [...]

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    [Leanplum didReceiveRemoteNotification:userInfo

fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    [Leanplum didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
    // Needs to be called if swizzling is disabled in Info.plist otherwise it won’t affect SDK if swizzling is enabled.
    [Leanplum didFailToRegisterForRemoteNotificationsWithError:error];
}

// Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler
{
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}

编辑:远程通知方式直接复制自Leanplum docs

当我从 AppDelegate.m 中删除此代码时,远程通知的一切正常。只有当我添加这些片段来呈现前台通知时,我才注意到 Leanplum 远程通知存在问题,例如打开的操作无法执行。

// Define UNUserNotificationCenter
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self; // <-- interferes with the remote notification SDK

...

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}

【问题讨论】:

    标签: ios objective-c react-native push-notification leanplum


    【解决方案1】:

    我遇到了同样的问题,但我发现在停用 swizzling 时不再调用一些深度 Leanplum 代码。

    将此添加到文件顶部

    #import <Leanplum/LPPushNotificationsManager.h>
    

    替换

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    {
      completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
    }
    

    有了这个:

    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    {
      [[LPPushNotificationsManager sharedManager].handler willPresentNotification:notification withCompletionHandler:completionHandler];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多