iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
0
Modern Web

我不用Expo啦,React Native!系列 第 18

[Day18] 提醒功能-3:計畫通知

今日關鍵字:notification


計畫通知內容

昨天的即時通知是在localNotif編寫通知內容

localNotif(anime: Anime) {
...

今天的計畫通知則是在scheduleNoti中編寫

scheduleNotif(anime: Anime) {
...

其實這兩個物件的內容幾乎是一樣的
計畫通知只多了一個屬性

date:new Date(...)

也就是設定這個通知何時會跳出

還有昨天多塞的anime這個屬性
也不能忘記補上去


通知只剩一個?

寫完在模擬機上運行的時候
突然跳出一個問題
如果我讓通知連跳複數次的話
留在手機上方的通知只剩一個

查詢套件的issue,並沒有相關的bug
只好換請教google看看有沒有方向
這篇文章的第一個回覆提到
You need to supply a different ID as the notification ID each time.

回頭看看範例的做法

export default class NotifService {
  constructor(onRegister, onNotification) {
    this.lastId = 0;
  ...

  localNotif(soundName) {
    this.lastId++;
    PushNotification.localNotification({

這樣id永遠是重複的1/images/emoticon/emoticon04.gif
當然會一直覆蓋前一則通知

正當我在考慮以timestamp作為id
簡單地解決這個問題時
突然看到一行備註

// (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID

看來只要刪除id屬性就不會有這個問題了

當然反過來說
如果通知只想留最新的一則
這個做法是沒什麼問題的/images/emoticon/emoticon13.gif


明天預計補上計算播放時間的功能

參考:react-native-push-notification


上一篇
[Day17] 提醒功能-2:即時通知
下一篇
[Day19] 提醒功能-4:計算播放時間
系列文
我不用Expo啦,React Native!33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言