如果您的應用只需要收到有關更改的通知,則此blog中建議的更好的方法是讓您的應用程序訂閱用戶驅動器的更改並在發生更改時收到通知。
假設您的應用程序託管的服務器上my-host.com
域和推送通知應該傳遞到HTTPS網頁鉤https://my-host.com/notification
:
String subscriptionId = UUID.randomUUID().toString();
Channel request = new Channel()
.setId(subscriptionId)
.setType("web_hook")
.setAddress("https://my-host.com/notification");
drive.changes().watch(request).execute();
有了這個,只要訂閱是活躍的,谷歌驅動器將在https://my-host.com/notification
觸發一個web-hook回調。然後該應用程序可以查詢變化飼料從上次同步點趕上:
changes = service.changes().list()
.setStartChangeId(lastChangeId).execute();
中進一步提到,如果你有興趣使用這項新功能,請參閱documentation。您可以通過Push Notifications Playground查看推送通知,並在Github查看源代碼。
推送通知有兩個問題:1它們不能用於觀看所有文件(請參閱:http://stackoverflow.com/questions/18144094/google-drive-sdk-push-notifications-watch-changes-on -all-files/18151189#18151189)2它們在1天后過期(這是不太嚴重的,因爲我可以再次註冊)。 –