react-native cloud messaging settings

Sean Hsieh-yoyo
3 min readMay 22, 2022

firebase messaging

Intro: 用來打APN 跟FCM 通知給app, 以下是一連串的設定,針對ios 還多出更多的步驟

ios:

step1:

你必須開啟你的xcworkspace, ex: /ios/myapp.xcworkspace. 打開之後:

  1. 選取你的專案
  2. 選取 Signing & Capabilities

3. 按下 “ +Capabilities”

4. 搜尋 push notification 然後點兩下

5. 重複第三步驟

6. 搜尋 Enable Background Modes

7. 這時你會看到 background modes 出現在下方, 勾取 background mode 跟 remote notifications

step2: Linking APNs with FCM (iOS)

Intro: APNs is required for both foreground and background messaging to function correctly on iOS

  1. 註冊一個key
  2. 註冊一個identifier
  3. 產生一個 provisioning profile

Hint: 這些步驟都需要擁有一個 Apple Developer Account, 他會需要 每年 3000 台幣的訂閱才可以使用這些服務

  1. 註冊一個key

description: 這個key 是用來給 FCM 權限去打 Apple Push Notification service.

  1. 按下Certificates, Identifiers & Profiles之後左邊會出現 一個 list 並且包含keys
keys on Certificates, Identifiers & Profiles

2. 按下藍色的加號,為他取上名字以及勾選 APNs。並且continue 之後,download file & copy key id

add keys
download keys file and copy key id

3. 剛剛 copy 的 key id 跟 下載的 download file 是用來加在 firebase console 上面的,到了Firebase console 之後

Project Settings > Cloud Messaging > 選取你的 iOS App > 按下upload

按下 TeamId 右邊一點下面的 upload

step3 : 註冊一個 App Identifier

Intro: 在 app 準備部署到production 之後 ,你必須創造一個新的App Identifier ,讓他可以Link 到你正在開發的 App (通常都會叫做 com.myCompany.myApp)

  1. 打開你的xcworkspace 在General Tab 下方的 bundle identifier

2. 回到 Apple Developer 那裡的Certificates, Identifiers & Account

description: 按下 Idenfiers > create a new one > 選取App IDs >

3. Capabilities 往下捲 勾取 Push Notification

step4:產生一個 provisioning profile

Intro: 一個 provisioning profile 讓Apple 跟你的 app 可以溝通,因為messaging 只能使用在實際的機器上(iOS),一個憑證可以讓app 在安裝的時候有正確的權限

在Profiles 的List 中,新增一個Profile,選擇 iOS App Development checkbox 然後Continue,然後就是選擇 Identifier 跟Certificates 的時候

hint : 關於創建 Certificates follow this document https://help.apple.com/developer-account/#/devbfa00fef7

  1. xcworkspace 選你的project
  2. Assign the provisioning profile

hint : 如果xcode 有登入並且 link to Apple Account, Xcode 可以自動sync 到profile 就不用做這些事情了xd

需要更詳細的setup : 官方文件 :

常見問題

  1. ios build code 遇到以下的error
PhaseScriptExecution [CP-User]\ Generate\ Specs /Users/

Solution:

在podfile 裡面增加下列幾行

installer.pods_project.build_configurations.each do |config|config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"end

2. 若是想要針對node_modules 某一個 package 做修改並且想要部署進去

Solution:

patch-package

pod file
post_install do |installer|
react_native_post_install(installer)
end

3. X-code build fail

rm -rf /Pods && rm Podfile.lock
pod install --repo-update
pod install

4. android build fail — 遇到 firebase 讀取不到的問題

buildscript {ext {minSdkVersion = 21compileSdkVersion = 31targetSdkVersion = 31}ext {compileSdkVersion   = 31targetSdkVersion    = 31//build.gradle

app/build.gradle

implementation platform('com.google.firebase:firebase-bom:28.0.1')implementation("com.google.firebase:firebase-iid")implementation 'com.google.firebase:firebase-messaging'// 需要加在dependencies 中

--

--