Android 23+ - 从备份

时间:2016-08-22 20:23:24

标签: android google-cloud-messaging android-6.0-marshmallow android-backup-service

我有一个使用Azure发送推送通知的应用。 Azure依次使用GCM发送到Android设备。

我注意到我的AndroidManifest.xml中有一条警告说明

  

在SDK版本23及更高版本上,您的应用数据将自动备份   up,并在应用安装上恢复。您的GCM注册无法使用   恢复,因此您必须确保将其从备份集中排除。   使用属性android:fullBackupContent指定@xml   资源,用于配置要备份的文件。

我已遵循此处https://developer.android.com/training/backup/autosyncapi.html?hl=in#configuring

的说明

但是我对如何从备份中排除GCM regID感到难过?这是我目前的设置。

清单

<application
        android:allowBackup="true"
        android:fullBackupContent="@xml/backup_scheme"
        ........

RES / XML / backup_scheme.xml

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <exclude domain="sharedpref" path=""/>
</full-backup-content>

我把什么作为路径?我应该在某个地方放置一个物理文件吗?

更新

所以我想我弄明白了。在我的RegistrationIntentService.java文件中,我将用户registrationID存储在字符串&#34; registrationID&#34;下的共享首选项中。所以我假设我使用以下内容......

<exclude domain="sharedpref" path="registrationID"/>

正确?

4 个答案:

答案 0 :(得分:6)

所以我明白了。在我的RegistrationIntentService.java文件中,我将用户registrationID存储在字符串“registrationID”下的共享首选项中。因此,请在backup_scheme.xml文件中使用以下内容...

<exclude domain="sharedpref" path="registrationID"/>

答案 1 :(得分:6)

我面临同样的问题而且我会接受接受答案,但事实证明它似乎不应该做什么。

我所做的是在创建backup_scheme.xml文件之前在清单中设置fullBackupContentProperty。当然,Android Studio抱怨警告,但为我提供了一个自动生成该文件的quickfix。

应用quickfix生成以下文件:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <!-- Remove the following "exclude" elements to make them a part of the auto backup -->
    <exclude
        domain="database"
        path="attendee.db" />
    <exclude
        domain="database"
        path="whova_messages.db" />
    <exclude
        domain="database"
        path="photo.db" />
    <exclude
        domain="database"
        path="agenda.db" />
    <exclude
        domain="database"
        path="ebb.db" />
    <exclude
        domain="database"
        path="vertical.db" />
    <exclude
        domain="database"
        path="whova.db" />
    <!-- Exclude the shared preferences file that contains the GCM registrationId -->
    <exclude
        domain="sharedpref"
        path="WhovaMessagePrefFile.xml" />
    <exclude
        domain="sharedpref"
        path="APP_RATER.xml" />
    <exclude
        domain="sharedpref"
        path="WhovaPrefFile.xml" />
</full-backup-content>

请注意<!-- Exclude the shared preferences file that contains the GCM registrationId -->

所以我的猜测是你必须排除包含GCM ID的整个共享首选项文件,而不仅仅是密钥。

太糟糕了,文档没有让它变得非常清晰,也太糟糕了,我们不得不从备份中排除整个文件。

答案 2 :(得分:2)

从@Simon答案进行进一步研究后,我认为当从Android Studio中的quickfix自动生成备份描述符xml文件时,我们仍然需要替换-execdir中指定的文件名。我检查了其中一个已实现Firebase Cloud Messaging的应用程序中的shared_prefs目录,并找到了以下GCM设置首选项文件:

enter image description here

所以backup_descriptor.xml将是:

Path

答案 3 :(得分:0)

要回答您的问题,应将文件指定为path。 因此,对于GCM注册,它是sharedpref域中称为com.google.android.gms.appid.xml

的XML
<exclude
    domain="sharedpref"
    path="com.google.android.gms.appid.xml"/>

但是,由于我没有发现任何地方对此进行记录,因此将来可能会更改,恕不另行通知。 因此,更好的方法是仅指定您实际要备份的内容。

我遇到了同样的问题,并在上​​面写了一篇小文章。 Why effects run on each update