Firebase Cloud Messaging ဖြင့် Push Notification စတင်တည်ဆောက်နည်း
Mobile app (Android, iOS, Flutter, React Native မည်သည့်ဟာမဆို) ထဲ Firebase Cloud Messaging ချိတ်ဆက်ပြီး device token စုဆောင်းခြင်းမှ background/foreground notification handle လုပ်ခြင်းအထိ push notification feature ကို အစအဆုံး တည်ဆောက်ပါမယ်။
ဖြေရှင်းမည့်ပြဿနာ
Mobile app တစ်ခုမှာ user engagement ပြန်တက်ဖို့ (order status update, new message, reminder) push notification feature မရှိမဖြစ် လိုအပ်ပါတယ်၊ ဒါပေမယ့် device token စီမံခန့်ခွဲမှု၊ platform-specific setup (Android/iOS ကွာခြားချက်)၊ foreground/background handling ကွဲပြားမှုတို့ကြောင့် ပထမဆုံးအကြိမ် implement လုပ်ရတာ ရှုပ်ထွေးတတ်ပါတယ်။
လိုအပ်ချက်များ
- Firebase project တစ်ခု (console.firebase.google.com မှာ အခမဲ့ create လုပ်နိုင်သည်)
- Build ချနိုင်သော Android, iOS, Flutter, သို့မဟုတ် React Native project တစ်ခု
- Android အတွက် physical device သို့မဟုတ် Google Play Services ပါသော emulator (iOS အတွက် push notification testing ဆိုရင် physical device လိုအပ်သည်)
Firebase Project ထဲ App ကို Register လုပ်ပါ
Firebase console ထဲမှာ project အသစ်တစ်ခု (သို့) ရှိပြီးသား project ကို ဖွင့်ပြီး app ကို register လုပ်ပါ — Android အတွက် package name (`applicationId`)၊ iOS အတွက် Bundle ID ကို အတိအကျ ထည့်ရပါမယ် — ဒါက app store ပေါ်တင်တဲ့ Bundle ID/package name နဲ့ တူညီနေရမှာမို့ အစောပိုင်းက အသေအချာ စစ်ဆေးထားသင့်ပါတယ်။ Config file (`google-services.json` for Android, `GoogleService-Info.plist` for iOS) ကို download ဆွဲပြီး project root ရဲ့ ညွှန်ပြထားတဲ့ folder ထဲ ထည့်ပါ။
FCM SDK ကို Install ပြီး Initialize လုပ်ပါ
Framework အလိုက် FCM SDK ကို package manager နဲ့ install ပါ။ Flutter/React Native လိုမျိုး cross-platform framework တွေမှာဆိုရင် Firebase SDK က Android/iOS layer နှစ်ခုစလုံးကို ချိတ်ဆက်ပေးထားပါတယ် — native Android/iOS project တစ်ခုချင်းစီအတွက် dependency ကို သီးသန့် ထပ်ထည့်စရာ မလိုတော့ပါဘူး။
$ flutter pub add firebase_core firebase_messagingawait Firebase.initializeApp();
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
badge: true,
sound: true,
);Device Token ကို ရယူပြီး Backend သို့ ပို့ပါ
Device တစ်ခုစီမှာ unique FCM token တစ်ခု ရှိပါတယ် — server ဘက်က notification ပို့ချင်တဲ့ device ကို ဒီ token ဖြင့်သာ ခွဲခြားနိုင်ပါတယ်။ App launch ဖြစ်တိုင်း token ကို ရယူပြီး backend database ထဲ (user account နှင့် ချိတ်ဆက်ကာ) သိမ်းထားရပါမယ် — token က periodic ပြောင်းလဲနိုင်လို့ (app reinstall, device backup restore, token expiry) `onTokenRefresh` listener ထားပြီး ပြောင်းတိုင်း backend ကို update ပို့ရပါမယ်။
String? token = await messaging.getToken();
// token ကို backend API ကို ပို့ပြီး user account နှင့် ချိတ်ဆက်ပါ
messaging.onTokenRefresh.listen((newToken) {
// token ပြောင်းတိုင်း backend ကို update ပို့ပါ
});Token တစ်ခုတည်းကို User များစွာက မမျှဝေပါနှင့်
Device တစ်ခုတည်းပေါ်မှာ user login/logout ပြောင်းလဲတဲ့အခါ token ကို user အဟောင်းနှင့် unlink ပြီး user အသစ်နှင့် ပြန်ချိတ်ဆက်ပါ — မဟုတ်ရင် device တစ်ခုတည်းကို user တစ်ယောက်ထက်ပိုပြီး login ဝင်ခဲ့ဖူးရင် notification က user မှား ရောက်သွားနိုင်ပါတယ်။
Foreground နှင့် Background Notification ကို Handle လုပ်ပါ
App က foreground (ဖွင့်ပြီး active) မှာ ရှိနေတုန်း notification ရောက်လာရင် OS ရဲ့ default notification tray မှာ အလိုအလျောက် ပေါ်မလာပါဘူး — app ကို app ကိုယ်တိုင်က listener နဲ့ ဖမ်းပြီး UI ထဲ manual ပြသရပါမယ်။ App က background/terminated ဖြစ်နေရင်တော့ OS က notification tray ထဲ အလိုအလျောက် ပြသပေးပါတယ်၊ user က notification ကို နှိပ်လိုက်ရင် app ကို ဖွင့်ပြီး correct screen ဆီ navigate လုပ်စေဖို့ handler ထားရပါမယ်။
// App ဖွင့်ပြီး active (foreground) ဖြစ်နေချိန်
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// UI ထဲ manual notification/snackbar ပြသပါ
});
// User က notification ကိုနှိပ်ပြီး app ကို ဖွင့်လိုက်ချိန် (background/terminated ကနေ)
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// notification data ပေါ်မူတည်ပြီး screen ဆီ navigate လုပ်ပါ
});ရရှိမည့်ရလဒ်
Backend server ကနေ device token တစ်ခုတည်းကို target လုပ်ပြီး notification ပို့လိုက်ရင် app foreground ဖြစ်ဖြစ်၊ background ဖြစ်ဖြစ် correct device ပေါ် ရောက်ရှိပြီး၊ user notification ကို နှိပ်လိုက်ရင် app ထဲက သက်ဆိုင်ရာ screen ဆီ တိုက်ရိုက် navigate ဖြစ်သွားမည့် end-to-end push notification pipeline တစ်ခု ရရှိမည်။
အဆင်မပြေပါက စစ်ဆေးရန်
- Notification လုံးဝ မရောက်ရင် google-services.json/GoogleService-Info.plist file က correct Firebase project ကနေ ဆွဲထားတာ (project mismatch) ဟုတ်မဟုတ် အရင်စစ်ပါ — Firebase project အမှား ချိတ်ထားလျှင် token ရသော်လည်း notification ပို့လို့ မရပါ။
- iOS ပေါ်မှာ notification မရောက်ရင် Apple Push Notification service (APNs) key/certificate ကို Firebase console ထဲ upload ထားခြင်း ရှိမရှိ စစ်ပါ — FCM သည် iOS အတွက် APNs ကို underlying transport အဖြစ် အားထားနေသည်၊ APNs key မရှိရင် Android မှာ အလုပ်ဖြစ်နေချိန် iOS မှာ တိတ်တိတ်ဆိတ်ဆိတ် fail ဖြစ်နိုင်ပါတယ်။
- Foreground notification UI မှာ မပေါ်ရင် onMessage listener ကို register လုပ်ထားခြင်း ရှိမရှိ၊ listener ကို widget lifecycle အစောပိုင်းမှာ setup လုပ်ထားခြင်း ရှိမရှိ ပြန်စစ်ပါ — background အတွက်တော့ OS default tray က ကိုင်တွယ်ပေးပေမယ့် foreground အတွက် app ကိုယ်တိုင် handle မလုပ်ရင် notification ဘာမှ မမြင်ရပါဘူး။
- Backend ကနေ token 100% valid ဖြစ်ပါလျက် intermittent ဖြစ်နေရင် expired/stale token များကို database ထဲက clean up မလုပ်ထားခြင်း ဖြစ်နိုင်ပါတယ် — FCM API ကနေ 'not registered' error ပြန်လာတဲ့ token များကို database ကနေ ပုံမှန် ဖယ်ရှားရပါမယ်။