使用Wix React Native Navigation V3设置React Native推送通知 - 应用程序关闭时出现问题

3

当用户点击通知时,我会发送一个导航到特定屏幕的通知。 如果应用程序已打开或在后台运行,则此功能完美运行,但是当应用程序关闭时,onNotification函数未被调用。 我正在使用React Native推送通知和Wix React Native Navigation V3。

我通过在onNotification内部放置控制台日志来发现问题,但从未被调用。

在index.js中,我有以下代码

import { start } from './App';

start();

在 App.js 中。
import React from 'react';
import { Navigation } from 'react-native-navigation';
import { Provider } from 'react-redux';
import configureStore from './src/configureStore';
import { configurePush } from './src/utils/push-notifications';

import Login from './src/components/views/Login';
import Home from './src/components/views/Home';
import Cart from './src/components/views/Cart';
import CartDetail from './src/components/views/Cart/Detail';
import Orders from './src/components/views/Orders';
... the rest of the screens


const store = configureStore();
configurePush(store);

export function registerScreens() {
  Navigation.registerComponent('provi.Login', () => (props) => (
  <Provider store={store}>
    <Login {...props} />
  </Provider>
  ), () => Login);

  Navigation.registerComponent('provi.Home', () => (props) => (
  <Provider store={store}>
    <Home {...props} />
  </Provider>
  ), () => Home);

  Navigation.registerComponent('provi.Cart', () => (props) => (
  <Provider store={store}>
    <Cart {...props} />
  </Provider>
  ), () => Cart);
... the rest of the screens

}

export function start() {
  registerScreens();
  Navigation.events().registerAppLaunchedListener(async () => {
    Navigation.setRoot({
      root: {
        stack: {
          children: [{
            component: {
              name: 'provi.Login',
              options: {
                animations: {
                  setStackRoot: {
                    enabled: true
                  }
                },
                topBar: {
                  visible: false,
                  drawBehind: true,
                  background: {
                    color: '#30DD70'
                  },
                },
                bottomTabs: {
                  visible: false
                }
              }
            }
          }],
        }
      }
    });
  });
}

接下来是通知的配置信息:

然后,通知的配置如下:

import PushNotificationIOS from "@react-native-community/push-notification-ios";
import { Navigation } from 'react-native-navigation';
import PushNotification from 'react-native-push-notification';
import DeviceInfo from 'react-native-device-info';
import fetchApi from "../store/api";
import { addNotification } from '../store/notifications/actions';
import { SENDER_ID } from '../constants';

export const configurePush = (store) => {
  PushNotification.configure({
      onRegister: function(token) {
          if (token) {
            const registerData = {
              token: token.token,
              uid: DeviceInfo.getUniqueID(),
              platform: token.os
            }
            // console.log(registerData);
            fetchApi('/notificaciones/register', 'POST', registerData).catch(err => console.log(err))
          }
      },
      onNotification: function(notification) {
        if (notification) {
          store.dispatch(addNotification(notification)); // Almacena la notification
          const action = notification.data.click_action;
          if (action === 'oferta') {
            const remotePost = notification.data.data;
            Navigation.setRoot({
              root: {
                stack: {
                  children: [{
                    component: {
                      name: 'provi.Home',
                      options: {
                        animations: {
                          setStackRoot: {
                            enabled: true
                          }
                        },
                        topBar: {
                          visible: true,
                          drawBehind: false,
                        },
                        passProps: {
                          test: 'test',
                          notification: remotePost
                        }
                      }
                    }
                  }],
                }
              }
            });
          } else if (action === 'seller') {
            const remoteSeller = notification.data.data;
            Navigation.push('Home', {
              component: {
                name: 'provi.Seller',
                passProps: {
                  id: remoteSeller._id,
                  featureImage: remoteSeller.featureImage
                },
                options: {
                  topBar: {
                    title: {
                      text: 'Nueva Marca!'
                    }
                  },
                  bottomTabs: {
                    visible: false,
                    drawBehind: true
                  }
                }
              }
            });
          } else if (action === 'sellerClosingSoon') {
            const remoteSeller = notification.data.data;
            Navigation.push('Home', {
              component: {
                name: 'provi.ClosingSoon',
                passProps: {
                  id: remoteSeller._id,
                  featureImage: remoteSeller.featureImage
                },
                options: {
                  topBar: {
                    title: {
                      text: 'Marcas que cierran pronto'
                    }
                  },
                  bottomTabs: {
                    visible: false,
                    drawBehind: true
                  }
                }
              }
            });
          }
        }
        notification.finish(PushNotificationIOS.FetchResult.NoData);
      },
      senderID: SENDER_ID,
      popInitialNotification: true,
      requestPermissions: true
  });
}

我希望至少能在控制台看到console.log,但现在并没有。

RNN V3与RN推送通知的正确设置是什么?

1个回答

0

他正在使用来自wix的react-native-push-notifications,而不是https://github.com/zo0r/react-native-push-notification/blob/master/component/index.android.js#L19。 它仍然能正常工作吗? - yashatreya
我认为机制应该对两者都是相同的。 - sergei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接