我在Feed组件内的构造函数中使用firebase .on('value')侦听器。当将值添加到firebase数据库时,它将触发,而当我使用this.showLocalNotification()时,它将起作用。但是,当我使用this.sendRemoteNotification()时,没有骰子。由于我可以在该函数中管理日志数据,因此该函数已被关闭,但是当应用程序处于前台,后台或完全不打开时,它无法显示通知。我已经在Xcode中检查了所有功能,正在iOS 8设备上运行此功能,并且正在使用react-native-fcm 16.0.0
。
这是我的Feed
组件,其构造函数包括firebase侦听器和推送通知功能。
registerKilledListener();
class Feed extends Component {
static navigationOptions = {
tabBarLabel: "Active Jobs",
tabBarIcon: ({ tintColor }) => (
<Icon name="newspaper" size={28} color={COLOR.ICON} />
)
}; // navigationOptions
constructor(props) {
super(props)
this.state = {
token: "",
tokenCopyFeedback: ""
}
database.ref("OpenRequests/").on("value", snapshot => {
let data = snapshot.val();
let obj = data[Object.keys(data)[Object.keys(data).length - 1]]
// this.showLocalNotification(obj)
this.sendRemoteNotification(obj)
});
}
async componentWillMount() {
console.log(this.props.navigation)
registerAppListener(this.props.navigation);
FCM.getInitialNotification().then(notif => {
this.setState({
initNotif: notif
});
if (notif && notif.targetScreen === "detail") {
setTimeout(() => {
this.props.navigation.navigate("Detail");
}, 500);
}
});
try {
let result = await FCM.requestPermissions({
badge: false,
sound: true,
alert: true
});
} catch (e) {
console.error(e);
}
FCM.getFCMToken().then(token => {
// console.log('Setting token: ', token)
// console.log("TOKEN (getFCMToken)", token);
this.setState({
token: token || ""
});
});
if (Platform.OS === "ios") {
FCM.getAPNSToken().then(token => {
// console.log("APNS TOKEN (getFCMToken)", token);
});
}
FCM.createNotificationChannel({
id: 'default',
name: 'Default',
description: 'used for example',
priority: 'high'
})
// topic example
// FCM.subscribeToTopic('sometopic')
// FCM.unsubscribeFromTopic('sometopic')
}
sendRemoteNotification(data) {
let body;
if (Platform.OS === "android") {
body = {
to: this.state.token,
data: {
custom_notification: {
title: "Simple FCM Client",
body: JSON.stringify(data),
sound: "default",
priority: "high",
show_in_foreground: true,
// targetScreen: "feed"
}
},
priority: 10
};
} else {
body = {
to: this.state.token,
notification: {
title: "Simple FCM Client",
body: JSON.stringify(data),
sound: "default"
},
// data: {
// targetScreen: "feed"
// },
priority: 10
};
}
firebaseClient.send(JSON.stringify(body), "notification");
}
showLocalNotification(data) {
console.log(data)
FCM.presentLocalNotification({
channel: 'default',
id: new Date().valueOf().toString(), // (optional for instant notification)
title: "INCOMING REQUEST", // as FCM payload
body: JSON.stringify(data), // as FCM payload (required)
sound: "bell.mp3", // "default" or filename
priority: "high", // as FCM payload
click_action: "com.myapp.MyCategory", // as FCM payload - this is used as category identifier on iOS.
badge: 10, // as FCM payload IOS only, set 0 to clear badges
number: 10, // Android only
ticker: "My Notification Ticker", // Android only
auto_cancel: true, // Android only (default true)
large_icon: "https://image.freepik.com/free-icon/small-boy-cartoon_318-38077.jpg", // Android only
icon: "ic_launcher", // as FCM payload, you can relace this with custom icon you put in mipmap
big_text: "Show when notification is expanded", // Android only
sub_text: "This is a subText", // Android only
color: "red", // Android only
vibrate: 300, // Android only default: 300, no vibration if you pass 0
wake_screen: true, // Android only, wake up screen when notification arrives
group: "group", // Android only
picture: "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png", // Android only bigPicture style
ongoing: true, // Android only
my_custom_data: "my_custom_field_value", // extra data you want to throw
lights: true, // Android only, LED blinking (default false)
show_in_foreground: true // notification when app is in foreground (local & remote)
});
}