【问题标题】:didReceiveLocalNotification, didReceiveRemoteNotification and willPresentNotification never triggered when notifications show up, React Native AppdidReceiveLocalNotification、didReceiveRemoteNotification 和 willPresentNotification 从未在通知出现时触发,React Native App
【发布时间】:2020-01-22 19:42:27
【问题描述】:

我正在尝试在 iOS 上的 react-native 应用程序中实现通知,但我遇到了一个奇怪的问题。该应用程序能够显示本地通知,并显示来自 firebase 的远程通知,但它不会按预期触发 react-native-push-notification 的事件“onNotification”。我已经通过许多论坛寻找解决方案,但我仍然没有找到解决方案,这就是我请求您帮助的原因。

经过调查,我意识到objective-c函数“didReceiveLocalNotification”和“didReceiveRemoteNotification”从未被触发,所以我认为这是“onNotification”也没有触发的原因。不幸的是,我完全是这门语言的初学者,所以我无法理解问题的原因。

查看“didReceiveLocalNotification”文档,我了解到该函数已被弃用,我应该使用“willPresentNotification”代替,但与其他函数一样,它从未被触发。

作为记录,我已按照本指南在 iOS 中处理通知: https://firebase.google.com/docs/cloud-messaging/ios/client。我正在 iOS 13、react-native 0.60.5、push-notifiaction-ios ^1.0.2、react-native-push-notification ^3.1.3 和 react-native-firebase ^5.5.6 上进行测试。

AppDelegate.m


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
    [FIRMessaging messaging].delegate = self;
  }
  if ([UNUserNotificationCenter class] != nil) {
    // iOS 10 or later
    // For iOS 10 display notification (sent via APNS)
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
        UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter]
        requestAuthorizationWithOptions:authOptions
        completionHandler:^(BOOL granted, NSError * _Nullable error) {
          // ...
        }];
  } else {
    // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [application registerUserNotificationSettings:settings];
  }

  [application registerForRemoteNotifications];

  // Load RootView ...

  return YES;
}
// sourceURLForBridge function ...

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
  [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event. You must call the completion handler after handling the remote notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  // Never called
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  // Never called
  [RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

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

PushNotification 和 firebase 配置


function configurePushNotification(){
  PushNotification.configure({
    onRegister: function(token) {
      console.warn("TOKEN:", token);
    },
    onNotification: function(notification) {
      console.warn("NOTIFICATION:", notification);

      // Never called

      notification.finish(PushNotificationIOS.FetchResult.NoData);
    },
    permissions: {
      alert: true,
      badge: true,
      sound: true
    },
    popInitialNotification: true,
    requestPermissions: true
  });
}
async function checkPermission(onReady) {
  const enabled = await firebase.messaging().hasPermission();
  if (enabled) {
    console.warn('checkPermission enabled');
    getToken(onReady);
  } else {
    console.warn('checkPermission disabled');
    requestPermission(onReady);
  }
}

async function requestPermission(onReady) {
  try {
    await firebase.messaging().requestPermission();
        // User has authorised
        console.warn('requestPermission try');
        getToken(onReady);
  } catch (error) {
        // User has rejected permissions
        console.error(error)
        console.warn('requestPermission rejected');
    }
}

async function getToken(onReady) {
  fcmToken = await AsyncStorage.getItem('fcmToken');
  if (!fcmToken) {
    fcmToken = await firebase.messaging().getToken();
    if (fcmToken) {
          // user has a device token
          await AsyncStorage.setItem('fcmToken', fcmToken);
      }
  }
  console.warn(fcmToken)
  if(onReady) onReady()
}

class App extends React.Component {

  async componentDidMount() {
    checkPermission()
    configurePushNotification()
  }

  render(){
    return (
      // View ...
    )
  }
};

感谢您的帮助!

编辑:经过调查,我意识到这些事件在我将 Firebase/Messaging 添加到我的 podfile 后停止工作。但根据 firebase 文档,这不是正常行为。关于这个原因的任何想法?

【问题讨论】:

    标签: ios objective-c react-native push-notification firebase-cloud-messaging


    【解决方案1】:

    我有办法。

    我并没有完全弄清楚为什么在 AppDelegate.m 中没有调用这些事件,但我注意到 Firebase 消息传递实现的相同事件(转到 Pods/Development Pods/RNFirebase/messaging 和通知中的代码) 在通知出现时被调用。所以我假设这段代码覆盖了 AppDelegate.m 中的代码。

    事实上,我并没有听到好的 Javascript 事件。此处指定的事件https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages 和此处https://rnfirebase.io/docs/v5.x.x/notifications/receiving-notifications 在iOS 中为我​​工作。但是这些事件不会在android上调用...

    所以我结束了在 Android 和 iOS 上以不同方式处理通知,使用:

    • Android 的 react-native-push-notification 的“onNotification”

    • 适用于 iOS 的 react-native-firebase 的“onNotification”和“onNotificationOpened”。

    【讨论】:

      【解决方案2】:

      本地通知由设备发送。

      远程通知 (APNS) 由您的服务器发送,由 Apple 服务器提供。 您向 Apple 发送通知,如果允许,Apple 会向设备发送通知。

      如果您使用像 Firebase 这样的服务,您向 Firebase 发送通知,Firebase 向 Apple 发送通知,Apple 向用户发送通知。

      ObjC 中的 Firebase 云消息传递使用方法 swizzling,as explained in documentation。所以,onNotification 没有被调用。

      我建议using this component for React Native。源代码中有一个示例应用,配置了AppDelegate.m。

      【讨论】:

      • 感谢您的回答! react-native-fcm 似乎已被弃用,无论如何我今晚将尝试示例应用程序,看看它是否可以帮助我。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-06
      • 2013-10-15
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多