React Native Push Notification onNotification回调不一致 [英] React Native Push Notification onNotification callback is inconsistent

查看:173
本文介绍了React Native Push Notification onNotification回调不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 react-native-push-notification 库在相当长的时间内收到本地通知.现在,我需要远程静默通知.基本上,我发送的通知将传递到onNotification回调,然后将本地通知发送到通知中心.

I've been using react-native-push-notification library for quite some time for local notifications. Now I need remote silent notifications. Basically I send a notification that gets delivered to onNotification callback and I send local notification to the notification center.

我希望onNotification回调在每次收到通知并通过单击通知打开应用程序时触发.问题是,取决于我在哪里调用PushNotification.configure({...}),在Android上它会在发送通知或单击通知时触发onNotification,但不会两者都触发!我的代码:

I expect onNotification callback to trigger every time I receive notification and I open an app by clicking on notification. The problem is, depending on where I call PushNotification.configure({...}), on Android it triggers onNotification when notification gets delivered OR when I click on it, but never both! My code:

// if here: calls onNotification when it's received
PushNotifications.configureNotifications();

export default class App extends React.Component {
    constructor(props) {
        // if here: calls onNotification when I click on it to open an app
        PushNotifications.configureNotifications();
        super(props);
    }

    render() { ... }
}

PushNotifications.configureNotifications()-它只是对库configure调用的简单包装.是的,试图在两个地方都保留它-不起作用.我在做什么错了?

PushNotifications.configureNotifications() - it's just a simple wrapper over the library configure call. Yes, tried to keep it in both places - doesn't work. What am I doing wrong?

我总是在推送通知中发送数据有效载荷.

I always send data payload in push notification.

推荐答案

文档说不要将配置放入React生命周期中.否则,Android的行为将不正确(这一点我实在太痛苦了!). 在这里,您有一个不错的教程: https://product.farewell.io/visible-react-native-push-notifications-that-work-on-bos-ios-and-android-5e90badb4a0f 很难解释,但是这种方法是完美的.使用一个类来初始化和调用方法.按照他的建议,在顶部组件中初始化课程.效果很好.通过本教程完成缺少的信息: https://shift.infinite.red/react- native-node-js-and-push-notifications-e851b279a0cd 玩得开心!

Documentation says to not put the configuration in the React life-cycle. Otherwise Android won't have the right behavior (I had so mush pain with this point !). Here you have a nice tutorial: https://product.farewell.io/visible-react-native-push-notifications-that-work-on-both-ios-and-android-5e90badb4a0f It's bad explained, but the approach is perfect. Use a class to initialize and call the methods. Initialize the class in the top component as he suggests. It works good. Complete the missing informations with this tutorial: https://shift.infinite.red/react-native-node-js-and-push-notifications-e851b279a0cd Have fun !

这篇关于React Native Push Notification onNotification回调不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆