2018年5月20日 星期日

react native push notification

push notification
ios:
ref:https://www.appcoda.com.tw/firebase-push-notifications/
step 1: 在apple developer裡的服務勾選notificaion,當下或之後上傳 csr,再下載認證
下載下來的是cer檔,加入keychain。
現在打開Keychain Access後,在”My Certificates”內,我們將可以看見你剛加入的認證檔。他應該會叫做:Apple Development IOS Push Services:<your.bundle.id>
認證檔export。(並非只export展開後的paivate key(這樣上傳firebase會error))
step 2:https://console.firebase.google.com/ 建立專案
將ios 加入friebase,輸入bundle id,下載plist檔
點選setting icon->專案設定 ,進入colud messaging tab。
在apn憑證上傳剛剛export出來的cer檔。
(意思上來說 如果你可以上傳的cer 包含product跟developer的話,就可以在開發跟release上都能push)

step 3: 安裝fcm in react native,照著安裝pod 相關流程,將從firebase的plist加入xcode 根目錄。
在header search path add $(SRCROOT)/../node_modules/react-native-fcm/ios
把nodemoudule/react-native-fcm/ios 裡的ios專案加進去libraries 裡,當然.a也要加進build phases 裡 。 
done。
在app中是不會顯示通知的,但是可以捕捉到 ,使用 
FCM.on(FCMEvent.Notification, notif => {
      alert("get notify");

    });
以下是捕捉到的資料 (notif)

{
  • "google.c.a.c_l":"label",
  • "opened_from_tray":true,
  • "google.c.a.c_id":"5887610821863657426",
  • "gcm.n.e":"1",
  • "google.c.a.e":"1",
  • "_completionHandlerId":"26E0AF7E-0985-4740-BF6C-E2065E11FA1B",
  • "gcm.message_id":"0:1526742758325202%e4c66395e4c66395",
  • "mykey":"12345",
  • "google.c.a.udt":"0",
  • "_notificationType":"notification_response",
  • "_actionIdentifier":"com.apple.UNNotificationDefaultActionIdentifier",
  • "aps":{
    • "alert":"hihi it's test may i use in app"
    },
  • "google.c.a.ts":"1526742758"
}
其中的opened_from_tray這個key只有在點擊notify banner才會有,
  • "mykey":"12345" 是傳送時自訂的資料。
  • (題外話:我在firebase裡只匯入product的憑證 developer的匯不進,說什麼環境不符,但我用developer測試可以work... 不過後來發佈卻不行收到通知,重新download product的cer,再export給firebase 就可以了)
android:
IOS好了之後,照fcm安裝說明,並把androidmanifest.xml的package name拿去建立firebase就好了,不用什麼vpn憑證,但是也是要把firebase下載下來的json檔放到android/app底下。(我android在外面收到通知不會跳出來,但往下滑是可以看見)