我试图检测我的反应原生应用是否由用户点击推送通知横幅(有关主题,请参阅this excellent SO answer)启动。如何使用react-native的PushNotificationIOS.getInitialNotification
我已经实现了Mark描述的模式,并且发现PushNotificationIOS.getInitialNotification
提供的“通知”对象真的很奇怪,至少在没有检索通知的情况下。检测到这种情况一直是PITA,我实际上很困惑。
从我所知道的,PushNotificationIOS.getInitialNotification
返回一个承诺;这个承诺应该在null
或实际的通知对象 - null
当没有通知等待用户时解决。这是我试图检测和支持的场景。
这就是为什么检测是如此痛苦;以下测试全部在没有通知的情况下运行:
// tell me about the object
JSON.stringify(notification);
//=> {}
// what keys does it have?
Object.keys(notification);
//=> [ '_data', '_badgeCount', '_sound', '_alert' ]
因此,它被串化为空,但它有四个键? ķ...
// tell me about the data, then
JSON.stringify(notification._data);
//=> undefined
// wtf?
这些怪异的事实阻挠我都了解,我这里有一个实际的通知作出反应,对情况的邮箱是空箱之间的辨别能力。基于这些事实,我以为我可以测试我想要的成员,但即使是最仔细的探测产生误报的100%的时间:
PushNotificationIOS.getInitialNotification()
.then((notification) => {
// usually there is no notification; don't act in those scenarios
if(!notification || notification === null || !notification.hasOwnProperty('_data')) {
return;
}
// is a real notification; grab the data and act.
let payload = notification._data.appName; // TODO: use correct accessor method, probably note.data() -- which doesn't exist
Store.dispatch(Actions.receivePushNotification(payload, true /* true = app was awaked by note */))
});
我每次运行此代码,它未能触发因为undefined is not an object (evaluating 'notification._data.appName')
逃生舱口盖和let payload
。
有人可以解释这里发生了什么吗? PushNotificationIOS.getInitialNotification
已损坏或已弃用?如何在JS中可以有一个评估为未定义的键?我如何检测这种情况?
经验丰富的javascripter,在这里很困惑。谢谢你的帮助。
BTW:使用反应母语v0.29.0
感谢您的解释!我很好奇你如何知道'PushNotification'没有'toString'。 – Tom