
Expo 推送通知實操指南從權限到點擊回傳【免費下載鏈接】expoAn open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.項目地址: https://gitcode.com/GitHub_Trending/ex/expoExpo 的expo-notifications庫為 React Native 應用提供跨平臺推送通知方案它封裝了 Android 的 FCM 與 iOS 的 APNs讓你用一套 API 收發通知。想給 App 加一條提醒不必自己對接推送服務裝好庫、拿到令牌就能發出第一條測試推送。環境準備npx expo install expo-notifications expo-constantsexpo-notifications負責權限、令牌和事件監聽expo-constants負責讀取項目配置里的projectId。再在app.json的plugins數組中加入expo-notifications插件會自動寫入兩端所需的原生配置。讓最小推送跑起來權限、令牌與測試發送在拿推送令牌ExpoPushToken相當于設備在 Expo 推送服務里的唯一地址之前必須完成兩件事拿到用戶授權、找到項目projectId。倉庫里的示例 apps/notification-tester/src/registerForNotifications.ts 就是這個流程的完整實現核心邏輯如下import Constants from expo-constants; import { getPermissionsAsync, requestPermissionsAsync, getExpoPushTokenAsync } from expo-notifications; async function registerForPushNotificationsAsync() { let { status } await getPermissionsAsync(); if (status ! granted) { ({ status } await requestPermissionsAsync()); } if (status ! granted) { throw new Error(notification permission denied); } const projectId Constants.expoConfig?.extra?.eas?.projectId ?? Constants.easConfig?.projectId; const token (await getExpoPushTokenAsync({ projectId })).data; return token; }應用啟動后在useEffect里調用它把令牌顯示在界面上方便測試。注意令牌必須綁定projectId生成兩端配置里的 projectId 不一致會導致后續發送被拒。發送測試通知只需要向 Expo 推送服務發一個 POST 請求無需自建服務端await fetch(https://exp.host/--/api/v2/push/send, { method: POST, headers: { Content-Type: application/json }, body: JSON.stringify({ to: expoPushToken, title: Original Title, body: And here is the body!, data: { someData: goes here }, }), });data是自定義數據字段后續可以從通知對象里原樣讀回是實現深度鏈接的入口。生產環境要把令牌上傳到你自己的后端并入庫而不是只存在 App 本地。平臺差異補齊Android 渠道與 iOS 憑據Android 8.0 起通知必須掛在渠道channel通知的分類容器下不建渠道推送可能不顯示。在注冊前調用setNotificationChannelAsync創建默認渠道if (Platform.OS android) { await Notifications.setNotificationChannelAsync(default, { name: default, importance: Notifications.AndroidImportance.MAX, vibrationPattern: [0, 250, 250, 250], lightColor: #FF231F7C, }); }憑據兩端也各不相同Android 需要 FCM v1 憑據google-services.jsoniOS 需要 APNs 密鑰通常通過eas credentials上傳到 EAS 項目構建時自動寫入。另外兩點容易忽略Android 上令牌可能隨系統重啟或配置變更而更新用addPushTokenListener監聽新令牌并回傳后端。iOS 上用戶拒絕權限彈窗后無法再發推送denied狀態要單獨處理引導用戶去系統設置里手動開啟。getDevicePushTokenAsync返回的是直連 FCM/APNs 的原始設備令牌走 Expo 推送服務時請用ExpoPushToken兩者不通用。前臺行為控制一個 Handler 與兩個監聽器App 在前臺時通知默認不彈橫幅展示行為由setNotificationHandler決定Notifications.setNotificationHandler({ handleNotification: async () ({ shouldShowBanner: true, shouldShowList: true, shouldPlaySound: false, shouldSetBadge: true, }), });兩個監聽器分工不同addNotificationReceivedListener只在前臺收到通知時觸發addNotificationResponseReceivedListener在用戶點擊通知時觸發且前后臺、冷啟動都有效const received Notifications.addNotificationReceivedListener(n { console.log(received, n.request.content.data); }); const response Notifications.addNotificationResponseReceivedListener(r { const data r.notification.request.content.data; if (data?.screen) { navigation.navigate(data.screen); } }); return () { received.remove(); response.remove(); };完整的事件對象結構和監聽器寫法可參考文檔 docs/pages/push-notifications/receiving-notifications.mdx。批量發送與送達確認不想寫服務端代碼時可用 Expo 賬號控制臺里的 Notifications 工具直接向指定令牌發推送。接口本身支持一次提交最多 100 條消息數組只要屬于同一個項目能顯著減少請求次數。發送后服務會先返回 push ticket接收憑證真正投遞到 FCM/APNs 之后可再查 push receipt送達回執確認結果。回執約 15 分鐘后可用24 小時后清除。如果回執里出現DeviceNotRegistered說明設備已卸載或關閉權限應停止向該令牌發送直到用戶重新注冊。?? 踩坑排錯五類常見現象令牌為空、注冊失敗 → 模擬器缺少 Google Play 服務 → 換物理設備或給 Android 模擬器裝上 Play 服務。發送返回 400 →to不是有效令牌或 payload 里的 projectId 與令牌不匹配 → 確認令牌由當前項目的 projectId 生成。前臺收到通知卻不彈橫幅 → handler 默認不展示 → 用setNotificationHandler顯式聲明shouldShowBanner: true。Android 通知不顯示 → 8.0 未創建通知渠道 → 先setNotificationChannelAsync建渠道再發推送。發送看似成功但設備收不到 → 憑據未配置或過期 → 檢查 EAS 項目里的 FCM/APNs 憑據并重新構建。收尾整條鏈路可以濃縮成三件事權限是門檻令牌是地址監聽器是回傳。把這三件跑通后自定義聲音、角標和深度鏈接都是在此基礎上疊加。更多細節見官方文檔 docs/pages/push-notifications/庫源碼在 packages/expo-notifications/。【免費下載鏈接】expoAn open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.項目地址: https://gitcode.com/GitHub_Trending/ex/expo創作聲明:本文部分內容由AI輔助生成(AIGC),僅供參考