3

我正在发送一个通知,当单击通知时将用户导航到特定屏幕。当应用程序在后台打开或运行时,这非常有效,但是,当应用程序关闭时,onNotification 不会被调用。我正在使用 react native 推送通知和 wix react native navigation V3。

我通过在通知中放置一个控制台日志来注意到这个问题,它从未被调用过。

在 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,但它没有发生。

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

4

1 回答 1

0

通知有两种类型:backgroundforeground

您使用foreground通知。但是当应用程序关闭并且您收到新通知时,您的回调将无法触发。

您应该使用类似的东西getInitialNotification来获取通知数据。

https://github.com/zo0r/react-native-push-notification/blob/master/component/index.android.js#L19

我希望这会有所帮助。

谢谢

于 2019-11-17T18:06:34.730 回答