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

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

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

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

Q&A

解決済

1回答

693閲覧

【registerForPushNotification is not defined】expoでプッシュ通知をさせたい

ok09

総合スコア11

React Native

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

0グッド

0クリップ

投稿2020/11/28 00:44

expo でプッシュ通知を実装しているのですが、以下のエラーが発生しました。
公式のdocumentを参考に書いているのですが、registerForPushNotificationを定義づけるように警告が出ています。

どなたかわかる方、ご指摘いただけると幸いです。

イメージ説明

HomeScreenのコード

ReactNative

1//一部抜粋 2 3Notifications.setNotificationHandler({ 4 handleNotification: async () => ({ 5 shouldShowAlert: true, 6 shouldPlaySound: false, 7 shouldSetBadge: false, 8 }), 9}); 10 11export default function HomeScreen() { 12const [expoPushToken, setExpoPushToken] = useState(''); 13const [notification, setNotification] = useState(false); 14const notificationListener = useRef(); 15const responseListener = useRef(); 16 17useEffect(() => { 18 registerForPushNotificationsAsync().then(token => setExpoPushToken(token)); 19 20 notificationListener.current = Notifications.addNotificationReceivedListener(notification => { 21 setNotification(notification); 22 }); 23 24 responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { 25 console.log(response); 26 }); 27 28 return () => { 29 Notifications.removeNotificationSubscription(notificationListener); 30 Notifications.removeNotificationSubscription(responseListener); 31 }; 32}, []); 33 34const user = useSelector(state => state.user); 35const {reminders} = user; 36const stringreminder = reminders.join(''); 37const FilteredItems = DummyItems.filter(function (dummy, index) { 38 if(stringreminder=="1"){ 39 return (dummy.title == "燃えるゴミ"); 40 }else if(stringreminder=="2"){ 41 return (dummy.title == "容器包装プラスチック"); 42 }else if(stringreminder==""){ 43 return (dummy.title == ""); 44 }else if(stringreminder=="12" || stringreminder=="21"){ 45 return (dummy.title == "燃えるゴミ" || dummy.title == "容器包装プラスチック"); 46 } 47 }); 48 49const items = FilteredItems.map((dummy, index) => { 50async function schedulePushNotification() { 51 await Notifications.scheduleNotificationAsync({ 52 content: { 53 title: '今日はゴミの日です!', 54 }, 55 trigger: { seconds: 2 }, 56 }); 57} 58 59async function registerForPushNotificationsAsync() { 60 let token; 61 if (Constants.isDevice) { 62 const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS); 63 let finalStatus = existingStatus; 64 if (existingStatus !== 'granted') { 65 const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); 66 finalStatus = status; 67 } 68 if (finalStatus !== 'granted') { 69 alert('Failed to get push token for push notification!'); 70 return; 71 } 72 token = (await Notifications.getExpoPushTokenAsync()).data; 73 console.log(token); 74 } else { 75 alert('Must use physical device for Push Notifications'); 76 } 77 78 if (Platform.OS === 'android') { 79 Notifications.setNotificationChannelAsync('default', { 80 name: 'default', 81 importance: Notifications.AndroidImportance.MAX, 82 vibrationPattern: [0, 250, 250, 250], 83 lightColor: '#FF231F7C', 84 }); 85 } 86 87 return token; 88} 89 90 return( 91 92//一部抜粋

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

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

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

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

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

guest

回答1

0

自己解決

呼び出している側のfunction記述の外に上記関数が記述されていたため、関数が宣言されている場所(スコープ)を確認してみたら解決できました!

投稿2020/11/29 01:53

ok09

総合スコア11

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問