RNFirebase v6 推送通知不会同时出现 iOS 和 Android
RNFirebase v6 Push Notifications are not coming both iOS&Android
我正在尝试从 firebase 控制台向我的 react-native 应用程序发送通知
据我所知,我遵循了这里糟糕的文档:https://invertase.io/oss/react-native-firebase/v6/messaging/quick-start
我安装了@react-native-firebase/app 和/messaging,这是我在组件中的代码:
componentDidMount() {
this.reqNotifications()
this.checkNotificationPermission()
}
reqNotifications() {
requestNotifications(['alert', 'badge', 'sound']).then(({status, settings}) => {
console.log('NOTIFICATION STATUS' + status)
});
}
async checkNotificationPermission() {
const enabled = await messaging().hasPermission();
if (enabled) {
console.log('APPROVED');
await messaging().registerForRemoteNotifications()
messaging().getToken().then(token => console.log('token: >> ' + token))
} else {
console.log('NOT APPROVED');
}
}
- 我正在通过 react-native-permissions 请求权限,权限请求是
工作.
- 我的 Apple APN 在 Apple 和 Firebase 控制台上正常
- 我正在通过代码上的 getToken() 方法获取我的令牌
成功.
但我无法从 firebase 向设备发送任何内容;前景和背景都没有发生任何事情。我尝试了 with-token 测试,也尝试了正常但没有,没有任何反应。
我将此代码添加到 componentDidMount:
messaging().onMessage(async remoteMessage => {
console.log('FCM Message Data:', remoteMessage.data);
});
据我了解,这订阅了云消息,当我从 firebase-console 发送一些云消息通知时,我应该得到控制台输出; 但什么也没发生。
我不知道我错过了什么,但我认为这个包有一个很大的更新,大部分文档都是针对以前版本的,我真的被困在这里感谢帮助
for rnfirebase.io V6
componentDidMount = async () => {
this.checkNotificationPermission();
await messaging().requestPermission({provisional: true});
await messaging().registerDeviceForRemoteMessages();
await this.getFCMToken();
if (Platform.OS === 'android') {
this.createAndroidNotificationChannel();
}
this.backgroundState();
this.foregroundState();
};
checkNotificationPermission = () => {
firebase
.messaging()
.hasPermission()
.then(enabled => {
if (!enabled) {
this.promptForNotificationPermission();
}
});
};
promptForNotificationPermission = () => {
firebase
.messaging()
.requestPermission({provisional: true})
.then(() => {
console.log('Permission granted.');
})
.catch(() => {
console.log('Permission rejected.');
});
};
createAndroidNotificationChannel() {
const channel = new firebase.notifications.Android.Channel(
'channelId',
'Push Notification',
firebase.notifications.Android.Importance.Max,
).setDescription('Turn on to receive push notification');
firebase.notifications().android.createChannel(channel);
}
foregroundState = () => {
const unsubscribe = messaging().onMessage(async notification => {
console.log('Message handled in the foregroundState!', notification);
});
return unsubscribe;
};
// Register background handler
backgroundState = () => {
messaging().setBackgroundMessageHandler(async notification => {
console.log('Message handled in the background!', notification);
});
};
我正在尝试从 firebase 控制台向我的 react-native 应用程序发送通知
据我所知,我遵循了这里糟糕的文档:https://invertase.io/oss/react-native-firebase/v6/messaging/quick-start
我安装了@react-native-firebase/app 和/messaging,这是我在组件中的代码:
componentDidMount() {
this.reqNotifications()
this.checkNotificationPermission()
}
reqNotifications() {
requestNotifications(['alert', 'badge', 'sound']).then(({status, settings}) => {
console.log('NOTIFICATION STATUS' + status)
});
}
async checkNotificationPermission() {
const enabled = await messaging().hasPermission();
if (enabled) {
console.log('APPROVED');
await messaging().registerForRemoteNotifications()
messaging().getToken().then(token => console.log('token: >> ' + token))
} else {
console.log('NOT APPROVED');
}
}
- 我正在通过 react-native-permissions 请求权限,权限请求是 工作.
- 我的 Apple APN 在 Apple 和 Firebase 控制台上正常
- 我正在通过代码上的 getToken() 方法获取我的令牌 成功.
但我无法从 firebase 向设备发送任何内容;前景和背景都没有发生任何事情。我尝试了 with-token 测试,也尝试了正常但没有,没有任何反应。
我将此代码添加到 componentDidMount:
messaging().onMessage(async remoteMessage => {
console.log('FCM Message Data:', remoteMessage.data);
});
据我了解,这订阅了云消息,当我从 firebase-console 发送一些云消息通知时,我应该得到控制台输出; 但什么也没发生。
我不知道我错过了什么,但我认为这个包有一个很大的更新,大部分文档都是针对以前版本的,我真的被困在这里感谢帮助
for rnfirebase.io V6
componentDidMount = async () => {
this.checkNotificationPermission();
await messaging().requestPermission({provisional: true});
await messaging().registerDeviceForRemoteMessages();
await this.getFCMToken();
if (Platform.OS === 'android') {
this.createAndroidNotificationChannel();
}
this.backgroundState();
this.foregroundState();
};
checkNotificationPermission = () => {
firebase
.messaging()
.hasPermission()
.then(enabled => {
if (!enabled) {
this.promptForNotificationPermission();
}
});
};
promptForNotificationPermission = () => {
firebase
.messaging()
.requestPermission({provisional: true})
.then(() => {
console.log('Permission granted.');
})
.catch(() => {
console.log('Permission rejected.');
});
};
createAndroidNotificationChannel() {
const channel = new firebase.notifications.Android.Channel(
'channelId',
'Push Notification',
firebase.notifications.Android.Importance.Max,
).setDescription('Turn on to receive push notification');
firebase.notifications().android.createChannel(channel);
}
foregroundState = () => {
const unsubscribe = messaging().onMessage(async notification => {
console.log('Message handled in the foregroundState!', notification);
});
return unsubscribe;
};
// Register background handler
backgroundState = () => {
messaging().setBackgroundMessageHandler(async notification => {
console.log('Message handled in the background!', notification);
});
};