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

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

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

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

Q&A

解決済

1回答

764閲覧

React NativeでのNotificationについて。【react/destructuring-assignment】

ok09

総合スコア11

React Native

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

0グッド

0クリップ

投稿2020/07/08 10:10

編集2020/07/08 11:40

前提・実現したいこと

React Native初心者で、簡単な通知アプリを作っています。
"expoPushToken: token" の部分で以下のエラーが発生しています。
初歩的なミスだとは思うのですが、解決の方法がわからず、教えていただけるとうれしいです。

発生している問題・エラーメッセージ

現在、以下のエラーが起きています。
Expected 'this' to be used by class async method 'sendPushNotification'. (class-method-use-this)
https://eslint.org/docs/rules/class-methods-use-this

試したこと

this.setState({ expoPushToken: token });
にも「Unused state field」というエラーが出ていたので、以下のように変更しました。

to: expoPushToken,

to: this.state.expoPushToken,

すると、今度は下記のような別のエラーが出ました。
Must use destructuring state assignment ( react/destructuring-assignment )
https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/destructuring-assignment.md

ソースコード

<途中から引用>

export default class AppContainer extends React.Component { componentDidMount() { this.registerForPushNotificationsAsync(); this.onReceivedListener = Notifications.addNotificationReceivedListener(this.onReceived); this.onResponseReceivedListener = Notifications.addNotificationResponseReceivedListener( this.onResponseReceived, ); } componentWillUnmount() { this.onReceivedListener.remove(); this.onResponseReceivedListener.remove(); } onReceived = (notification) => { console.log(notification); this.setState('notification'); }; onResponseReceived = (response) => { console.log(response); }; registerForPushNotificationsAsync = async () => { if (Constants.isDevice) { const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS); let finalStatus = existingStatus; if (existingStatus !== 'granted') { const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); finalStatus = status; } if (finalStatus !== 'granted') { alert('Failed to get push token for push notification!'); return; } const { data: token } = await Notifications.getExpoPushTokenAsync(); console.log(token); this.setState({ expoPushToken: token }); } else { alert('Must use physical device for Push Notifications'); } if (Platform.OS === 'android') { Notifications.setNotificationChannelAsync('default', { name: 'default', importance: Notifications.AndroidImportance.MAX, vibrationPattern: [0, 250, 250, 250], lightColor: '#FF231F7C', }); } }; async sendPushNotification(expoPushToken) { const message = { to: this.state.expoPushToken, sound: 'default', title: 'Original Title', body: 'And here is the body!', data: { data: 'goes here' }, }; await fetch('https://exp.host/--/api/v2/push/send', { method: 'POST', headers: { Accept: 'application/json', 'Accept-encoding': 'gzip, deflate', 'Content-Type': 'application/json', }, body: JSON.stringify(message), }); }

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

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

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

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

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

wilf

2020/07/08 10:22

コードの周りは ``` で囲むようにすると インデントが反映されるので、やってみていただきたいです。
ok09

2020/07/08 11:40

反映させました!確認お願いします!
guest

回答1

0

自己解決

expoのドキュメントを参考に、registerForPushNotificationAsync箇所を下記のように修正しましたところ、eslintの警告は解決しました。Simulatorは依然エラーが起きていますが。

registerForPushNotificationsAsync = async () => { let token; if (Constants.isDevice) { const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS); let finalStatus = existingStatus; if (existingStatus !== 'granted') { const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); finalStatus = status; } if (finalStatus !== 'granted') { alert('Failed to get push token for push notification!'); return; } token = (await Notifications.getExpoPushTokenAsync()).data; console.log(token); } else { alert('Must use physical device for Push Notifications'); }

投稿2020/07/11 03:03

ok09

総合スコア11

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問