我在 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)
});
}