ba-cafe

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

OneSignalInit.tsx (1227B)


      1 "use client";
      2 import { useEffect } from 'react';
      3 import OneSignal from 'react-onesignal';
      4 import { supabase } from "@/hooks/useAuth";
      5 
      6 export default function OneSignalInit() {
      7   useEffect(() => {
      8     const initOneSignal = async () => {
      9       try {
     10         await OneSignal.init({
     11           appId: process.env.NEXT_PUBLIC_ONESIGNAL_APP_ID!,
     12           allowLocalhostAsSecureOrigin: true, 
     13           // パスを絶対パス(またはルートからの相対パス)で明示する
     14           serviceWorkerPath: "/OneSignalSDKWorker.js",
     15           serviceWorkerParam: { scope: "/" },
     16           welcomeNotification: {
     17             title: "Cafe Timer",
     18             message: "先生、通知設定が完了しました!",
     19           },
     20         });
     21 
     22         // 初期化直後にログイン状態を確認
     23         const { data: { user } } = await supabase.auth.getUser();
     24         if (user) {
     25           // OneSignalのExternal IDとしてSupabaseのUser IDをセット
     26           // これによりDBのIDと通知先が強固に紐付く
     27           await OneSignal.login(user.id);
     28         }
     29       } catch (error) {
     30         console.error("OneSignal init error", error);
     31       }
     32     };
     33 
     34     initOneSignal();
     35   }, []);
     36 
     37   return null;
     38 }