Реактивное родовое уведомление IOS для firebase - PullRequest
2 голосов
/ 01 ноября 2019

Я пытаюсь использовать FB для push-уведомлений. Все прекрасно работает на Android Id бутон IOS есть некоторые проблемы. Основная проблема в том, что я не могу получить уведомление. -> Bud иногда уведомление приходит правильно, и все работает хорошо, когда я переустановить приложение уведомление не будет поступать на iPhone. Это работает случайно.

Я использую

"react-native": "0.59.4"
"react-native-firebase": "^5.5.6"

Файл моего модуля

  pod 'Firebase/Core', '~> 6.3.0'
  pod 'Firebase/Messaging', '~> 6.3.0'

AppDelegate.m

#import <Firebase.h>
#import <UserNotifications/UserNotifications.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"
...

if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
}
[RNFirebaseNotifications configure];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings
 settingsForTypes: (UIUserNotificationTypeBadge |
                    UIUserNotificationTypeSound |
                    UIUserNotificationTypeAlert)
 categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

// Register the supported interaction types.

UIUserNotificationType types = UIUserNotificationTypeBadge |

UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings =

[UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

...

- (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];
}

А вот как я использую его в коде:

import firebase from 'react-native-firebase';

const getToken = () => {
  firebase.messaging().getToken().then((token) => {
    console.log('XXXXX getToken: ', token);
    firebase.messaging().ios.registerForRemoteNotifications();
  });
};

const requestPermission = () => {
  try {
    firebase.messaging().requestPermission().then((xx) => {
      console.log('XXXX requestPermission', xx);
    });
  } catch (error) {
    console.log('XXXX requestPermission User has rejected permissions');
  }
};

export const checkPermission = () => {
  // firebase.messaging().ios.registerForRemoteNotifications();

  firebase.messaging().hasPermission().then((permission) => {
    console.log('XXXXX hasPermission check: ', permission);
    if (permission) {
      console.log('XXXX hasPermission TRUE');
      getToken();
    } else {
      console.log('XXXX hasPermission FALSE');

      requestPermission();
    }
  }).catch((e) => {
    console.log('XXXXX hasPermission ERROR: ', e);
  });
};


export const listeners = () => {
  console.log("XXXXX Listeners setup OK!!");

  firebase.notifications().onNotificationDisplayed((notification) => {
    console.log("XXXXX onNotificationDisplayed", notification);
  });

  firebase.notifications().onNotification((notification) => {
    console.log("XXXXX LISTENER onNotification", notification);

  });
  firebase.notifications().onNotificationOpened((notificationOpen) => {
    console.log("XXXXX LISTENER onNotificationOpened");
  });
  firebase.notifications().getInitialNotification().then((notificationOpen)=> {
    console.log("XXXXX LISTENER getInitialNotification OK", notificationOpen);
  }).catch((error)=> {
    console.log("XXXXX LISTENER getInitialNotification ERROR");
  });
  firebase.messaging().onMessage((message) => {
    console.log("XXXXX LISTENER onMessage");
  });
}

Я импортирую этот файл в основной компонент

 componentDidMount() {
    checkPermission();
    listeners();
  }

Все отлично работает на Android Я могу получитьуведомления и извлекать все данные. но для ios я все настроил подготовленным и он должен работать по логам: enter image description here Все выглядит хорошо ... теперь я пытаюсь отправить уведомление на свой телефон, и ничего не происходит. Но если я попытаюсь отправить его непосредственно на этот токен, он РАБОТАЕТ, я получу уведомление.

enter image description here

, и иногда он начинает работать, и я получаю уведомлениеКогда я переустанавливаю приложение, оно не работает снова.

Есть решение?

1 Ответ

0 голосов
/ 07 ноября 2019

Мое решение: обновить Cocopod. Все проблемы исчезли

  pod 'Firebase/Core', '~> 6.5.0'
  pod 'Firebase/Messaging', '~> 6.5.0'
...