远程推送通知无法按预期方式运行[IOS] [React Native]。没有错误

时间:2019-05-15 14:31:51

标签: ios reactjs react-native firebase-cloud-messaging react-native-firebase

首先,该应用仅在前台时才会收到通知。如果应用程序在后台,则不会显示通知,并且侦听器不会按预期输入,但是,当我打开应用程序时,侦听器会收到我在后台发送的通知。

我只需将APNs密钥添加到firebase控制台,然后重新下载GoogleService-info.plist并将其添加到项目中,并检查“功能”下的“背景模式->远程通知”和“推送通知”项在XCode中,并且我没有将Pod用于RNF

第二件事是,每当我发送通知或仅数据消息时,应用程序始终将其作为通知听(触发 onNotification ),以获取正文,标题,声音等未定义,并且“通知”键位于内部数据中,即使我从未在通知中发送数据并且它获取了新的键(不是由我发送),e:“ 1”

我正在使用“ react-native”:“ 0.59.4”和“ react-native-firebase”:“ 5.3.1”,未使用Pods,并在iPad 2中使用邮递员将推送发送到FCM对其进行了测试

这是我的AppDelegate.m:

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <RNSplashScreen.h>
#import <ReactNativeConfig.h>
#import <NewRelicAgent/NewRelic.h>
#import "Firebase.h"
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;
  NSString *newrelicToken = [ReactNativeConfig envFor:@"NEWRELIC_IOS_TOKEN"];

  [NewRelicAgent startWithApplicationToken:newrelicToken];

  [FIRApp configure];

  [RNFirebaseNotifications configure];

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  //jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"Prestadores"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  [RNSplashScreen show];
  return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
  [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
                                                       fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
  [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

@end

这是我的组件:

export default class NotificationsHandler extends Component<Props> {
  componentDidMount() {
    const {
      onNavigate,
      onGetAssigned,
      onFCMMessage,
      onSelect,
      token,
      cuit,
    } = this.props;

    if (Config.FCM === 'true') {
      this.registerFCM();
      // Build a channel
      const channel = new firebase
        .notifications
        .Android
        .Channel('mobile-prestadores', 'Prestadores Channel', firebase.notifications.Android.Importance.Max)
        .setDescription('Swiss Medical Prestadores');

      // Create the channel
      firebase.notifications().android.createChannel(channel);
      this.addFCMMessageListener();
    }
    firebase.notifications().getInitialNotification()
      .then((notificationOpen) => {
        if (notificationOpen) {
          // const action = notificationOpen.action;
          // const notification = notificationOpen.notification;
          const notification = { ...notificationOpen.notification };
          let service = { ...notification.data };
          token ? this.manageClickNotification(service) : this.props.onNavigate(constants.navigation.login);
        }
      });
  }

  componentWillUnmount() {
    this.removeListeners();
  }

  registerFCM() {
    const {
      onFCMRegister,
    } = this.props;
    return requestGCMPermissions().then((fcm) => {
      if (fcm) {
        firebase.messaging().getToken().then((fcmToken) => {
          if (fcmToken) {
            console.log(fcmToken);
            onFCMRegister(fcmToken);
          } else {
            firebase.messaging().onTokenRefresh((newToken) => {
              console.log(fcmToken);
              onFCMRegister(newToken);
            });
          }
        });
      }
    });
  }

  addFCMMessageListener() {
    const {
      onUpdatePush,
      onFCMMessage,
      onNavigate,
      onGetAssigned,
      onSelect,
      token,
      cuit,
    } = this.props;
    this.messageListener = firebase.messaging().onMessage((message) => {
      let service = message.data;
      this.manageRecieveNotifications(service);
    });
    this.notificationDisplayedListener =
      firebase.notifications().onNotificationDisplayed((notification) => {
        let service = notification.data;
        this.manageRecieveNotifications(service);
      });
    this.notificationListener =
      firebase.notifications().onNotification((notification) => {
        let service = notification.data;
        service = formatters.pushFormat(service);
        this.manageRecieveNotifications(service);
      });
    this.notificationOpenedListener =
      firebase.notifications().onNotificationOpened((notificationOpen) => {
        const notification = { ...notificationOpen.notification };
        let service = { ...notification.data };
        service = formatters.pushFormat(service);
        token ? this.manageClickNotification(service) : onNavigate(constants.navigation.login);
      });
  }

  removeListeners() {
    const { listener } = this.props.location;
    if (listener) {
      navigator.geolocation.clearWatch(this.watchId);
    }
    if (this.messageListener) {
      this.messageListener();
    }
    if (this.notificationDisplayedListener) {
      this.notificationDisplayedListener();
    }
    if (this.notificationOpenedListener) {
      this.notificationOpenedListener();
    }
    if (this.notificationListener) {
      this.notificationListener();
    }
  }

  render() {
    return(
      <View style={styles.container}>
        {this.props.children}
      </View>
    )
  }
} 

我正在发送此邮件:

{
    "to": "DEVICE FCM",
    "priority": "high",
    "content_available": true,
    "data": {
        "title":"title example",
        "body":"body example"
    },
    "notification": {
        "title":"title example",
        "body":"body example",
        "sound":"default"
    }
}

它没有显示任何错误,但它的正文和标题为 undefined 和通知键,位于数据键内(即使我未发送任何数据),也位于通知键内它会返回 e:“ 1” ,如下所示:

Notification {
  _body: undefined,
  _data: {
    from: "591699987834",
    notification: {
      body: "test",
      e: "1",
      title: "prueba",
      __proto__: Object
    },
    __proto__: Object
  },
  _ios: {
    _notification: Notification,
    _alertAction: undefined, 
    _attachments: Array(0), 
    _badge: undefined, 
    _category: undefined, 
  },
  _notificationId: "-LemY1Pf-aM0PK4a3oRb",
  _sound: undefined,
  _subtitle: undefined,
  _title: undefined,
  android: (...),
  body: (...),
  data: (...),
  ios: (...),
  notificationId: (...),
  sound: (...),
  subtitle: (...),
  title: (...),
  __proto__: Object
}

0 个答案:

没有答案