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

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

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

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

Q&A

0回答

1960閲覧

React NativeのNotificationについて。【Native module cannot be null】

ok09

総合スコア11

React Native

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

0グッド

1クリップ

投稿2020/07/11 03:15

編集2022/01/12 10:55

React Native初心者で、簡単な通知アプリを作っています。
simulatorに、以下のエラーが発生しています。
初歩的なミスだとは思うのですが、なかなか解決の方法がわからず、困っています。

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

現在、以下のエラーが起きています。
Invariant violation: Native module cannot be null
イメージ説明

ソースコード

import React from 'react'; import { View, Button, Platform } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import Constants from 'expo-constants'; import * as Notifications from 'expo-notifications'; import * as Permissions from 'expo-permissions'; import firebase from 'firebase'; import GomiHomeScreen from './src/screens/GomiHomeScreen'; import GomiUsageScreen from './src/screens/GomiUsageScreen'; import GomiSettingScreen from './src/screens/GomiSettingScreen'; import SoundSettingScreen from './src/screens/SoundSettingScreen'; import ReminderSettingScreen from './src/screens/ReminderSettingScreen'; Notifications.setNotificationHandler({ handleNotification: async () => ({ shouldShowAlert: true, shouldPlaySound: false, shouldSetBadge: false, }), }); 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 () => { 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'); } if (Platform.OS === 'android') { Notifications.setNotificationChannelAsync('default', { name: 'default', importance: Notifications.AndroidImportance.MAX, vibrationPattern: [0, 250, 250, 250], lightColor: '#FF231F7C', }); } }; endPushNotification = async (expoToken) => { const message = { to: 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), }); } render() { return ( <NavigationContainer> const Tab = createBottomTabNavigator(); <Tab.Navigator tabBarOptions={{ activeTintColor: '#e91e63', labelStyle: { fontSize: 16 }, }} > <Tab.Screen name="ホーム" component={GomiHomeScreen} /> <Tab.Screen name="使い方" component={GomiUsageScreen} /> <Tab.Screen name="通知設定" component={ReminderSettingScreen} /> <Tab.Screen name="通知音" component={SoundSettingScreen} /> </Tab.Navigator> </NavigationContainer> ); } }

###補足情報(FW/ツールのバージョンなど)

"react": "~16.9.0",
"expo": "~37.0.3",
"expo-constants": "~9.0.0",
"expo-notifications": "~0.1.1",
"expo-permissions": "~8.1.0",

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2020/07/11 03:42

javaとjavascliptは別物です。 javaといいはるなら、コンパイル通るようにしてください
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問