ReactNativeでアプリを作っています。(Android)
Expoのプッシュ通知機能を使っているのですが、プッシュ通知を受け取ってプッシュ通知をタップした時
アプリが起動していない時 → アプリが起動してルートコンポーネントのpropsに通知データがもらえる。
アプリが起動していてバックグラウンドのとき → Notifications.addListenerに登録したハンドラが通知を受信したとき呼ばれるが、通知をタップしてアプリをフォアグラウンドにしたときにハンドラが呼ばれない。
アプリが起動していてバックグラウンドのときに、受信した通知をタップしてアプリをフォアグラウンドにしたときにタップした通知の情報を処理したいと思っています。
どうしたらよいでしょうか?
import React from 'react' import AppNavigator from './navigators/AppNavigator.js' import { store } from './modules/index' import { Provider } from 'react-redux' import { KeyboardAvoidingView, StyleSheet } from 'react-native' // import './ReactotronConfig' import Expo, { Permissions, Notifications } from 'expo'; // YellowBox(黄色いエラー)を非表示にする console.disableYellowBox = true async function registerForPushNotificationsAsync() { const { status: existingStatus } = await Permissions.getAsync( Permissions.NOTIFICATIONS ); let finalStatus = existingStatus; // only ask if permissions have not already been determined, because // iOS won't necessarily prompt the user a second time. if (existingStatus !== 'granted') { // Android remote notification permissions are granted during the app // install, so this will only ask on iOS const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); finalStatus = status; } // Stop here if the user did not grant permissions if (finalStatus !== 'granted') { return; } // Get the token that uniquely identifies this device let token = await Notifications.getExpoPushTokenAsync(); } class App extends React.Component { constructor (props) { super(props) console.log('LLLLLLLAUNCH', props) } componentDidMount () { registerForPushNotificationsAsync(); this._notificationSubscription = Notifications.addListener(this._handleNotification); } render () { return ( <KeyboardAvoidingView behavior='padding' style={styles.wrapper}> <Provider store={store}> <AppNavigator /> </Provider> </KeyboardAvoidingView> ) } _handleNotification = (notification) => { // this.setState({notification: notification}); console.log('LAUNCH FROM NOTIFICATION!!!', notification) }; static handleNotification(notification, navigation) { if (notification.data.screen && notification.origin === 'selected') { navigation.navigate(notification.data.screen); } Toast.show(notification.data.message); } } const styles = StyleSheet.create({ wrapper: { flex: 1 } }) export default App Expo.registerRootComponent(App)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。