Javascript 单击“我的应用程序”中的特定页面时响应本机Firebase推送通知
我想让我的推送通知能够带我到一个特定的页面“指纹”一旦点击。到目前为止,点击推送通知时除了在后台打开应用程序外,什么都不做。我已经在网上搜索了两天,试错都没有用。任何帮助都将不胜感激Javascript 单击“我的应用程序”中的特定页面时响应本机Firebase推送通知,javascript,react-native,firebase-cloud-messaging,Javascript,React Native,Firebase Cloud Messaging,我想让我的推送通知能够带我到一个特定的页面“指纹”一旦点击。到目前为止,点击推送通知时除了在后台打开应用程序外,什么都不做。我已经在网上搜索了两天,试错都没有用。任何帮助都将不胜感激 import React, {Component} from 'react'; import {Platform, StyleSheet, Text, View, Alert, AsyncStorage, Button} from 'react-native'; import firebase fr
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Alert, AsyncStorage, Button} from 'react-native';
import firebase from 'react-native-firebase';
type Props = {};
export default class testapp extends Component<Props> {
async componentDidMount() {
this.checkPermission();
this.createNotificationListeners(); //add this line
}
componentWillUnmount() {
this.notificationListener;
this.notificationOpenedListener;
}
//1
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
//3
async getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
// user has a device token
console.log('fcmToken:', fcmToken);
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
console.log('fcmToken:', fcmToken);
}
//2
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
console.log('onNotification:');
const localNotification = new firebase.notifications.Notification({
sound: 'sampleaudio',
show_in_foreground: true,
})
.setSound('sampleaudio.wav')
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setBody(notification.body)
.android.setChannelId('fcm_FirebaseNotifiction_default_channel') // e.g. the id you chose above
.android.setSmallIcon('@drawable/ic_launcher') // create this icon in Android Studio
.android.setColor('#000000') // you can set a color here
// .setClickAction(()=>alert('test'))
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
});
const channel = new firebase.notifications.Android.Channel('fcm_FirebaseNotifiction_default_channel', 'UniFinalApp', firebase.notifications.Android.Importance.High)
.setDescription('Demo app description')
.setSound('sampleaudio.wav');
firebase.notifications().android.createChannel(channel);
/*
Code would probably go in the section below
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
console.log('onNotificationOpened:');
// Alert.alert(title, body)
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
console.log('getInitialNotification:');
Alert.alert(title, body)
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
console.log("JSON.stringify:", JSON.stringify(message));
});
}
render() {
// const {navigate}=this.props.navigation;
return (
<View >
</View>
);
}
}
import React,{Component}来自'React';
从“react native”导入{平台、样式表、文本、视图、警报、异步存储、按钮};
从“react native firebase”导入firebase;
类型Props={};
导出默认类testapp扩展组件{
异步组件didmount(){
这是checkPermission();
this.createNotificationListeners();//添加此行
}
组件将卸载(){
这是通知听者;
此.notificationOpenedListener;
}
//1
异步检查权限(){
const enabled=等待firebase.messaging().hasPermission();
如果(已启用){
这个。getToken();
}否则{
这个.requestPermission();
}
}
//3
异步getToken(){
让fcmToken=等待AsyncStorage.getItem('fcmToken');
如果(!fcmToken){
fcmToken=await firebase.messaging().getToken();
如果(fcmToken){
//用户有一个设备令牌
log('fcmToken:',fcmToken);
等待AsyncStorage.setItem('fcmToken',fcmToken);
}
}
log('fcmToken:',fcmToken);
}
//2
异步请求权限(){
试一试{
等待firebase.messaging().requestPermission();
//用户已授权
这个。getToken();
}捕获(错误){
//用户已拒绝权限
log(“权限被拒绝”);
}
}
异步createNotificationListeners(){
/*
*在前台收到特定通知时触发
* */
this.notificationListener=firebase.notifications().onNotification((通知)=>{
const{title,body}=通知;
log('onNotification:');
const localNotification=new firebase.notifications.Notification({
声音:“sampleaudio”,
在前景中显示:真,
})
.setSound('sampleaudio.wav')
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setBody(notification.body)
.android.setChannelId('fcm_firebasenotification_default_channel')//例如,您在上面选择的id
.android.setSmallIcon('@drawable/ic_launcher')//在android Studio中创建此图标
.android.setColor('#000000')//您可以在此处设置颜色
//.setClickAction(()=>警报('test'))
.android.setPriority(firebase.notifications.android.Priority.High);
firebase.notifications()
.displayNotification(本地通知)
.catch(err=>console.error(err));
});
const channel=new firebase.notifications.Android.channel('fcm\u firebasenotification\u default\u channel','UniFinalApp',firebase.notifications.Android.Importance.High)
.setDescription('演示应用程序说明')
.setSound('sampleaudio.wav');
firebase.notifications().android.createChannel(通道);
/*
代码可能会出现在下面的部分中
*如果您的应用程序位于后台,则您可以在单击/点击/打开通知时进行监听,如下所示:
* */
this.notificationOpenedListener=firebase.notifications().onNotificationOpened((notificationOpen)=>{
const{title,body}=notificationOpen.notification;
log('onNotificationOpened:');
//警报。警报(标题、正文)
});
/*
*如果您的应用程序已关闭,您可以检查它是否通过单击/点击/打开通知打开,如下所示:
* */
const notificationOpen=wait firebase.notifications().getInitialNotification();
如果(通知打开){
const{title,body}=notificationOpen.notification;
log('getInitialNotification:');
警报。警报(标题、正文)
}
/*
*为前台中的仅数据有效负载触发
* */
this.messageListener=firebase.messaging().onMessage((message)=>{
//进程数据消息
log(“JSON.stringify:,JSON.stringify(message));
});
}
render(){
//const{navigate}=this.props.navigation;
返回(
);
}
}
的文档中;有一节介绍如何处理通知中的数据。你可以做的是在通知中发送数据,告诉应用程序该做什么。例如:
firebase.notifications().getInitialNotification()
.then((notificationOpen: NotificationOpen) => {
if (notificationOpen) {
// App was opened by a notification
const notification: Notification = notificationOpen.notification;
const data = notificationOpen.notification._data;
if (data.action === 'openChat') {
//Code to open Chat screen
}
}
});
或者,您可以使用通知中的数据设置一些标志,然后在用户进入某个屏幕(如登录后)后,检查标志以查看是否需要执行某些操作 我正试图得到这样的通知,但它也不起作用。这对于后台工作意味着当应用程序关闭时
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const {_notificationId} = notificationOpen.notification.data
if (_notificationId) {
redirect('chatform', {id: _notificationId})
}
}
我正在使用云函数“admin”向特定设备发送远程通知,该通知基于我获得的FCM令牌及其在设备中的接收情况,但当我单击此通知时,它只需在“主屏幕”中打开应用程序,那么如何处理这些通知以在特殊屏幕(如通知屏幕)中打开@兔子87