我只是遵循Native Notify网站在React Native中实现通知。
我在iOS设备上用Expo start测试它。首先,我注册了我的Push令牌。然后试着发送通知,我可以在我的设备上看到通知。
然而,当我尝试发送一个数据对象时,出现了一个问题。我完全按照指示,我的App.js看起来像在例子中:
import { StyleSheet, Text, View } from "react-native";
import React, { useState, useEffect } from "react";
import { getPushDataObject } from "native-notify";
import registerNNPushToken from "native-notify";
export default function App() {
registerNNPushToken(7141, "Xy4gdSRWQlE1DUBmwuS3x0");
let pushDataObject = getPushDataObject();
useEffect(() => {
console.log(pushDataObject);
}, [pushDataObject]);
return (
<View style={styles.container}>
<Text>Hello world.</Text>
</View>
);
}
在通知中发送数据时,我在控制台中打印出数据,但也得到这个警告:
WARN Possible Unhandled Promise Rejection (id: 0):
Error: Another async call to this method is in progress. Await the first Promise.
Error: Another async call to this method is in progress. Await the first Promise.
at construct (native)
at apply (native)
at _construct (http://100.64.1.14:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false:3348:28)
at Wrapper (http://100.64.1.14:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false:3310:25)
at construct (native)
at _createSuperInternal (http://100.64.1.14:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false:96959:322)
at call (native)
at CodedError (http://100.64.1.14:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false:96967:26)
我首先在一个已经有Expo通知系统的应用程序中实现了它,并得到了相同的消息,所以我在一个新创建的应用程序中从0开始尝试。它指的是哪个承诺?怎能等待那应许呢?
请这样使用
import registerNNPushToken, {getPushDataObject} from 'native-notify';
import React, {useEffect, useState} from 'react';
import {Text, View} from 'react-native';
export default function App() {
registerNNPushToken(7141, 'Xy4gdSRWQlE1DUBmwuS3x0');
const [pushDataObject, setPushData] = useState(null);
useEffect(() => {
myfunction();
if (pushDataObject != null) {
console.log(pushDataObject);
}
}, [pushDataObject]);
const myfunction = async () => {
let data = await getPushDataObject();
setPushData(data);
};
return (
<View style={styles.container}>
<Text>Hello world.</Text>
</View>
);
}
你的代码的问题是你调用getPushDataObject没有async和await。