android – Google Drive REST API Notifications不适用于app文件夹中的更改
作者:互联网
根据文档,应该可以使用setSpaces(“appDataFolder”)注册更改我的应用程序的app文件夹的通知通道.
但是,我在设置频道时只收到初始同步通知,但在我更改app文件夹中的内容时没有更改通知.
如果我使用setSpaces(“驱动器”)或完全省略setSpaces()并在常规驱动器空间中更改某些内容,我会收到更改通知.
我没有找到关于在app文件夹中查看更改的任何内容,所以我希望有人可以帮助我.
这就是我设置频道的方式,其中mDrive是com.google.api.services.drive.Drive的完全初始化和授权的实例.
channelId = UUID.randomUUID().toString();
channelExpiration = System.currentTimeMillis() + CHANNEL_LIVETIME_MILLIS;
Channel channel = new Channel();
channel.setType("web_hook");
channel.setId(channelId);
channel.setAddress(DRIVE_API_CALLBACK_RECEIVER_URL);
channel.setToken("...");
channel.setExpiration(channelExpiration);
Channel result = mDrive.changes().watch(channel).setSpaces("appDataFolder").execute();
解决方法:
你有没有设置这个范围?确保您有足够广泛的范围来包含’appDataFolder’.我的意思是你应该能够从这里获得一些结果(从here开始):
/**
* Print metadata for the Application Data folder.
*
* @param service Drive API service instance.
*/
private static void printApplicationDataFolderMetadata(Drive service) {
try {
File file = service.files().get("appfolder").execute();
System.out.println("Id: " + file.getId());
System.out.println("Title: " + file.getTitle());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
我认为以上可能是你的问题.无论您设置范围,请确保包含drive.appfolder或更正式
https://www.googleapis.com/auth/drive.appfolder
此外,你在检查结果是否为空?你真的应该将Channel result = …行放在try {} catch(IOExeption e){}中,如果在这个例子中有一个(从here开始),则打印错误.
/**
* Watch for all changes to a user's Drive.
*
* @param service Drive API service instance.
* @param channelId Unique string that identifies this channel.
* @param channelType Type of delivery mechanism used for this channel.
* @param channelAddress Address where notifications are delivered.
* @return The created channel if successful, {@code null} otherwise.
*/
private static Channel watchChange(Drive service, String channelId,
String channelType, String channelAddress) {
Channel channel = new Channel();
channel.setId(channelId);
channel.setType(channelType);
channel.setAddress(channelAddress);
try {
return service.changes().watch(channel).execute();
} catch (IOException e) {
e.printStackTrace();
// ADD A LOG OR PRINT STATEMENT HERE
Log.e("DRIVEAPI", "Error: " + e.toString())
}
return null;
}
您的代码应该如下所示:
channelId = UUID.randomUUID().toString();
channelExpiration = System.currentTimeMillis() + CHANNEL_LIVETIME_MILLIS;
Channel channel = new Channel();
channel.setType("web_hook");
channel.setId(channelId);
channel.setAddress(DRIVE_API_CALLBACK_RECEIVER_URL);
channel.setToken("...");
channel.setExpiration(channelExpiration);
try {
Channel result = mDrive.changes().watch(channel).setSpaces("appDataFolder").execute();
if(result != null) {
// do whatever you want with result Channel
} else {
Log.e("DRIVEAPI", "Error: result is null for some reason!");
}
} catch (IOException e) {
e.printStackTrace()
Log.e("DRIVEAPI", "Error: " + e.toString());
}
标签:rest,android,notifications,google-drive-sdk 来源: https://codeday.me/bug/20190711/1433022.html