import React, { Component } from 'react'
import { Text, View,Button } from 'react-native'
import PushNotification from 'react-native-push-notification';
import firebase from 'react-native-firebase';
import { Header } from 'react-native/Libraries/NewAppScreen';
export default class App extends Component {
getPermission = () => {
firebase.messaging().requestPermission().then(() => {
console.log('Has')
}).catch(error => {
console.log('Permission was Rejected',error)
})
}
async componentDidMount(){
// firebase.messaging().hasPermission().then(enable => {
// if(enable){
// console.log('Has Permission')
// }
// else{
// this.getPermission()
// }
// })
let fcmToken;
if(!fcmToken){
debugger
fcmToken =await firebase.messaging().getToken();
console.log(fcmToken)
if(fcmToken){
// await AsyncStorage.setItem('fcmToken')
}
}
PushNotification.configure({
onNotification: function(notification) {
console.log('notification',notification)
}
});
}
sendNotifications = async () => {
const FIREBASE_API_KEY ="APi_key";
const message = {
registration_ids:["react native firebase id"],
notification : {
title : 'notification',
body : "Hello There","vibrate" : 1,
"sound" : 1,
"show_in_underground" : true,
"priority" : "high",
"content_avaliable" : true
}
}
let header = new Headers({
"Content-Type": "application/json",
"Authorization" : "key="+ FIREBASE_API_KEY
})
let response = await fetch("https://fcm.googleapis.com/fcm/send",{method : 'POST',header,body : JSON.stringify(message)})
console.log('here',response)
}
render() {
return (
<View>
<Text> textInComponent </Text>
<Button title="Send Notification from app" onPress={() => this.sendNotifications()}></Button>
</View>
)
}
}
当函数被调用时,我得到这个响应
type: "default" status: 401 ok: false statusText: undefined headers: Headers {map: {…}} url: "https://fcm.googleapis.com/fcm/send" _bodyInit: Blob {_data: {…}} _bodyBlob: Blob {_data: {…}} __proto__: Object
我会感谢您的帮助。