質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

Q&A

解決済

1回答

1299閲覧

(ReactNative)Expoでプッシュ通知をタップしてアプリを起動したときに通知のデータを受け取る方法

wabisuke2718

総合スコア96

React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

0グッド

1クリップ

投稿2018/05/10 11:22

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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

この投稿
を見て謎が解けました。

つまりdataフィールドを埋めてませんでした。

投稿2018/05/10 13:02

wabisuke2718

総合スコア96

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問