Thuta Learning
React Native
AdvancedMobile Developmentအလယ်အလတ်

Push Notifications Basics

React Nativeသင်ခန်းစာ 2325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Push Notifications Basics ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်
  • Accessibility, failure နဲ့ lifecycle cases စမ်းနိုင်ရန်
  • Maintainable, testable နဲ့ performant React Native code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Push Notifications Basics ကို ရိုးရိုးလေးနားလည်မယ်

Push notification ရဲ့ high-level workflow — (1) App က permission တောင်း, (2) device unique 'push token' ရယူ, (3) push token ကို backend server ဆီ ပို့သိမ်း, (4) backend က event တစ်ခုခု ဖြစ်ချိန် (message ရောက်, order confirm) push token ကို သုံးပြီး Expo/FCM/APNs push service ကနေတဆင့် notification ပို့။ Local notification (device ကိုယ်တိုင် schedule) နဲ့ Remote/Push notification (server ကနေ trigger) ကို ခွဲခြားနားလည်ဖို့ အရေးကြီးပါတယ်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Push token ကို backend database ထဲ သိမ်းစရာ မလိုတယ်လို့ ထင်ခြင်း — remote push ပို့ဖို့ backend ကနေ token ကို သိထားရပါမည်

ဖြစ်လေ့ရှိတဲ့အမှား

Local vs Push notification ကို ရောနားလည်ပြီး backend architecture design ကို ရှုပ်ထွေးအောင်လုပ်ခြင်း

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Push Notifications Basics ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။

Mental Model
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Push Notifications Basics ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

javascript
import * as Notifications from 'expo-notifications';

async function registerForPushNotifications() {
  const { status } = await Notifications.requestPermissionsAsync();
  if (status !== 'granted') return null;

  const tokenData = await Notifications.getExpoPushTokenAsync();
  return tokenData.data; // send this to your backend to store
}

// Local notification example (no backend needed)
await Notifications.scheduleNotificationAsync({
  content: { title: 'Reminder', body: 'Practice React Native today!' },
  trigger: { seconds: 60 },
});

Push Notifications Basics ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. JavaScript stateimport * as Notifications from 'expo-notifications';

    Push notification ရဲ့ high-level workflow — (1) App က permission တောင်း, (2) device unique 'push token' ရယူ, (3) push token ကို backend server ဆီ ပို့သိမ်း, (4) backend က event တစ်ခုခု ဖြစ်ချိန် (message ရောက်, order confirm) push token ကို သုံးပြီး Expo/FCM/APNs push service ကနေတဆင့် notification ပို့။ Local notification (device ကိုယ်တိုင် schedule) နဲ့ Remote/Push notification (server ကနေ trigger) ကို ခွဲခြားနားလည်ဖို့ အရေးကြီးပါတယ်။

  2. Native component + layout}

    Push Notifications Basics ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။

  3. Android / iOS UI});

    Push Notifications Basics example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Execution Flow
  1. JavaScript state
  2. Native component + layout
  3. Android / iOS UI

Success

Push Notifications Basics ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

Failure

Push token ကို backend database ထဲ သိမ်းစရာ မလိုတယ်လို့ ထင်ခြင်း — remote push ပို့ဖို့ backend ကနေ token ကို သိထားရပါမည်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Push notification ရဲ့ high-level workflow — (1) App က permission တောင်း, (2) device unique 'push token' ရယူ, (3) push token ကို backend server ဆီ ပို့သိမ်း, (4) backend က event တစ်ခုခု ဖြစ်ချိန် (message ရောက်, order confirm) push token ကို သုံးပြီး Expo/FCM/APNs push service ကနေတဆင့် notification ပို့။ Local notification (device ကိုယ်တိုင် schedule) နဲ့ Remote/Push notification (server ကနေ trigger) ကို ခွဲခြားနားလည်ဖို့ အရေးကြီးပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

expo-notifications ကို install ပြီး Notifications.requestPermissionsAsync() ခေါ်ရင် permission dialog ပေါ်ပြီး, granted ရင် Notifications.getExpoPushTokenAsync() ကနေ unique token တစ်ခု ရပါတယ် — ဒီ token ကို backend database ထဲ user record နဲ့ တွဲသိမ်းထားရမှာပါ။ Local notification (ဥပမာ reminder) ကတော့ backend မလိုဘဲ Notifications.scheduleNotificationAsync() နဲ့ device ကိုယ်တိုင် schedule လုပ်လို့ရပါတယ်။

Code နမူနာ

javascript
import * as Notifications from 'expo-notifications';

async function registerForPushNotifications() {
  const { status } = await Notifications.requestPermissionsAsync();
  if (status !== 'granted') return null;

  const tokenData = await Notifications.getExpoPushTokenAsync();
  return tokenData.data; // send this to your backend to store
}

// Local notification example (no backend needed)
await Notifications.scheduleNotificationAsync({
  content: { title: 'Reminder', body: 'Practice React Native today!' },
  trigger: { seconds: 60 },
});
You should see
Permission ရပြီးရင် push token string တစ်ခု ရမည်, local notification ကို ၆၀ စက္ကန့်အကြာ trigger ဖြစ်လာမည်။

၅ မိနစ် စမ်းကြည့်

expo-notifications install ပြီး local notification (scheduleNotificationAsync) ကို ကိုယ်တိုင် schedule ကြည့်ပါ (Expo Go ပေါ်မှာ)။

သတိလေးတစ်ချက်

Push notification permission ကို app ဖွင့်ချက်ချင်း တောင်းရင် user တွေက deny ဖန်များပါတယ် — feature ကို တကယ်သုံးမယ့်အချိန် (ဥပမာ chat feature ပထမဆုံးသုံးချိန်) မှသာ contextually တောင်းတာက accept rate ပိုကောင်းပါတယ်။

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Push Notifications Basics example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Push Notifications Basics example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
javascript
import * as Notifications from 'expo-notifications';

async function registerForPushNotifications() {
  const { status } = await Notifications.requestPermissionsAsync();
  if (status !== 'granted') return null;

  const tokenData = await Notifications.getExpoPushTokenAsync();
  return tokenData.data; // send this to your backend to store
}

// Local notification example (no backend needed)
await Notifications.scheduleNotificationAsync({
  content: { title: 'Reminder', body: 'Practice React Native today!' },
  trigger: { seconds: 60 },
});

ရလဒ်: Push Notifications Basics ရဲ့ React နဲ့ native platform behavior ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Push Notifications Basics example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

Quick Quiz 1 / 3

Push Notifications Basics ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • JavaScript state → Native component + layout → Android / iOS UI
  • Push Notifications Basics ကို permission, loading, empty, offline, timeout, cancellation, retry, app background/foreground နဲ့ stale-data behavior ပါသော lifecycle-aware flow အဖြစ်တည်ဆောက်ပါ။ Subscription, timer နဲ့ request cleanup ကိုမမေ့ပါနှင့်။
  • Push token ကို backend database ထဲ သိမ်းစရာ မလိုတယ်လို့ ထင်ခြင်း — remote push ပို့ဖို့ backend ကနေ token ကို သိထားရပါမည်
  • Push Notifications Basics example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

React Native Documentation — React Native

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Push token ကို backend database ထဲ သိမ်းစရာ မလိုတယ်လို့ ထင်ခြင်း — remote push ပို့ဖို့ backend ကနေ token ကို သိထားရပါမည်
  • Local vs Push notification ကို ရောနားလည်ပြီး backend architecture design ကို ရှုပ်ထွေးအောင်လုပ်ခြင်း
  • Simulator တစ်ခု၏ happy path မှန်ရုံဖြင့် real devices, release builds, permissions, accessibility, offline နဲ့ lifecycle paths အားလုံးမှန်ပြီဟုယူဆခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Push Notifications Basics example ကို Android/iOS, small/large screens, font scaling, dark mode, keyboard, screen reader, slow/offline network, denied permissions နဲ့ app background/foreground cases ဖြင့်စမ်းပါ။ DevTools, tests နဲ့ release build ကိုလိုအပ်သလိုအသုံးပြုပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Permission ရပြီးရင် push token string တစ်ခု ရမည်, local notification ကို ၆၀ စက္ကန့်အကြာ trigger ဖြစ်လာမည်။

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Push Notifications Basics | Thuta Learning