我已经使用创建了推送通知react-native Firebase
and react-native-push-notification
。我已经实现了所有类型的通知,如本地、日程背景和退出。但我已通过以下方式发送了推送通知FCM当我的应用程序处于退出状态时。所以我在控制台上显示了一个警告警告没有为关键 ReactNativeFirebaseMessagingHeadlessTask 注册任务。那我该如何解决呢。
code:
import React, {Fragment, useEffect} from 'react';
import {StyleSheet, View, Text, Button} from 'react-native';
import PushNotification from 'react-native-push-notification';
import messaging from '@react-native-firebase/messaging';
//1
const checkPermission = () => {
messaging()
.hasPermission()
.then((enabled) => {
if (enabled) {
getToken();
} else {
requestPermission();
}
})
.catch((error) => {
console.log('error checking permisions ' + error);
});
};
//2
const requestPermission = () => {
messaging()
.requestPermission()
.then(() => {
getToken();
})
.catch((error) => {
console.log('permission rejected ' + error);
});
};
//3
const getToken = () => {
messaging()
.getToken()
.then((token) => {
console.log('push token ' + token);
})
.catch((error) => {
console.log('error getting push token ' + error);
});
};
const NotificationTwo = () => {
useEffect(() => {
checkPermission();
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Message handled in the background!', remoteMessage);
});
});
const calledLocalNotify = () => {
PushNotification.localNotification({
/* Android Only Properties */
title: 'Hello world Local Notify', // (optional)
message: 'Successfully!, Implement the Local Notifications', // (required)
});
};
const calledLocalScheduleNotify = () => {
PushNotification.localNotificationSchedule({
//... You can use all the options from localNotifications
message: 'Successfully!, Implement the Local Schedule Notifications', // (required)
date: new Date(Date.now() + 60 * 1000), // in 60 secs
allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
});
};
return (
<View style={styles.container}>
<Text>Push Notification</Text>
<View style={styles.button}>
<Button
color="green"
title="Local Notification"
onPress={calledLocalNotify}
/>
</View>
<View style={styles.button}>
<Button
color="purple"
title="Local Schedule Notification"
onPress={calledLocalScheduleNotify}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
button: {
margin: 10,
},
});
export default NotificationTwo;