Javascript 如何使用react native';s PushNotificationIOS.getInitialNotification
我试图检测我的react native应用程序是否是由用户点击推送通知横幅启动的(请参见主题中的) 我已经实现了Mark描述的模式,并发现由Javascript 如何使用react native';s PushNotificationIOS.getInitialNotification,javascript,push-notification,react-native,Javascript,Push Notification,React Native,我试图检测我的react native应用程序是否是由用户点击推送通知横幅启动的(请参见主题中的) 我已经实现了Mark描述的模式,并发现由PushNotificationIOS.getInitialNotification提供的“通知”对象非常奇怪,至少在没有要检索的通知的情况下是如此。侦破这起案件是一件很麻烦的事,我实际上很困惑 据我所知,PushNotificationIOS.getInitialNotification返回一个承诺;当没有通知等待用户时,应该使用null或实际的通知对象-
PushNotificationIOS.getInitialNotification
提供的“通知”对象非常奇怪,至少在没有要检索的通知的情况下是如此。侦破这起案件是一件很麻烦的事,我实际上很困惑
据我所知,PushNotificationIOS.getInitialNotification
返回一个承诺;当没有通知等待用户时,应该使用null
或实际的通知对象--null
来解析此承诺。这就是我试图检测和支持的场景
这就是为什么这是一个如此痛苦的检测;以下测试都是在没有要查找的通知时运行的:
// tell me about the object
JSON.stringify(notification);
//=> {}
// what keys does it have?
Object.keys(notification);
//=> [ '_data', '_badgeCount', '_sound', '_alert' ]
所以它字符串化为空,但它有四个键?K
// 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 */))
});
每次我运行此代码时,它都无法触发转义图案填充并抛出let payload
,因为undefined不是一个对象(评估'notification.\u data.appName')
有人能解释一下这是怎么回事吗?PushNotificationIOS.getInitialNotification
是否已损坏或弃用?在JS中,如何可能有一个计算结果为未定义的键?如何检测这种情况
有经验的javascripter,这里相当困惑。谢谢你的帮助
顺便说一句:使用react native v0.29.0时,
通知
是推送通知
,而不是普通对象,这就是为什么它会字符串化为空对象,因为没有为它实现自定义toString
在我看来,对象是在没有可用通知的情况下创建的(如果还没有,应该报告)
无论如何,要解决这个问题,您的支票实际上应该是:
if(!notification || !notification.getData()) {
return;
}
更新:问题已在0.31中修复-有关更多详细信息,请参阅。感谢您的解释!我很好奇,你怎么知道对于
推送通知
没有toString
。