gpt4 book ai didi

javascript - React-Native Firebase 推送通知 onClick 到我的应用程序中的特定页面

转载 作者:行者123 更新时间:2023-11-30 06:14:57 25 4
gpt4 key购买 nike

我想让我的推送通知能够在点击后将我带到特定页面“指纹”。截至目前,推送通知在单击时除了在后台打开应用程序外什么都不做。我在网上搜索了两天,现在试错了也无济于事。任何帮助将不胜感激。

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

最佳答案

React-native-firebase 的文档中;有一节介绍如何处理来自通知的数据。您可以做的是在通知中发送数据,告诉应用程序该做什么。例如:

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

或者您可以使用通知中的数据设置一些标志,然后一旦用户进入某个屏幕(例如登录后),检查标志以确定是否需要执行某些操作。

关于javascript - React-Native Firebase 推送通知 onClick 到我的应用程序中的特定页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com