2017-04-13 36 views
1

停止手錶通道不能正常工作,但它不是一個錯誤響應,甚至允許傳播過夜。我仍然收到一個日曆更改的5個通知。有時候6.有時候3.它是零星的。我們還接收到8秒後進行同樣的動作進行第二輪通知。有時候6秒。有時第三組隨機數。也是零星的。共收到10個獨特的信息用於通過網絡瀏覽器創建一個單一的日曆。谷歌日曆API手錶渠道不停止

回答

0

您可以對特定的日曆資源腕錶請求無限量,谷歌將始終返回相同的日曆資源ID相同的日曆,但在請求產生的uuid會有所不同,正因爲如此,你會會爲您製作的每個觀看請求接收多個通知。停止從特定日曆資源的所有通知的方法之一,就是聽取通知,拉出「X-goog-通道ID」和「x-goog-資源ID」從通知標題,並在Channels.stop要求使用它們。

{ 
    "id": string, 
    "resourceId": string 
} 

每次執行收看請求時,你應該從響應持久化數據,並檢查uuid或資源ID已經存在,如果是不要再執行該資源ID收看請求(如你不想收到多個通知)。

例如

app.post("/calendar/listen", async function (req, res) { 
    var pushNotification = req.headers; 
    res.writeHead(200, { 
     'Content-Type': 'text/html' 
    }); 
    res.end("Post recieved"); 
    var userData = await dynamoDB.getSignInData(pushNotification["x-goog-channel-token"]).catch(function (err) { 
     console.log("Promise rejected: " + err); 
    }); 
    if (!userData) { 
     console.log("User data not found in the database"); 
    } else { 
     if (!userData.calendar) { 
      console.log("Calendar token not found in the user data object, can't perform Calendar API calls"); 
     } else { 
      oauth2client.credentials = userData.calendar; 
      await calendarManager.stopWatching(oauth2client, pushNotification["x-goog-channel-id"], pushNotification["x-goog-resource-id"]) 
     } 
    } 
}; 

calendarManager.js

module.exports.stopWatching = function (oauth2client, channelId, resourceId) { 
    return new Promise(function (resolve, reject) { 
     calendar.channels.stop({ 
      auth: oauth2client, 
      resource: { 
       id: channelId, 
       resourceId: resourceId 
      } 
     }, async function (err, response) { 
      if (err) { 
       console.log('The API returned an error: ' + err); 
       return reject(err); 
      } else { 
       console.log("Stopped watching channel " + channelId); 
       await dynamoDB.deleteWatchData(channelId) 
       resolve(response); 
      } 
     }) 
    }) 
}