Android 23+ - 从备份中排除 GCM 注册 ID

Android 23+ - Exclude GCM Registration ID from backup

我有一个使用 Azure 发送推送通知的应用程序。 Azure 反过来使用 GCM 发送到 Android 设备。

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

On SDK version 23 and up, your app data will be automatically backed up, and restored on app install. Your GCM regid will not work across restores, so you must ensure that it is excluded from the back-up set. Use the attribute android:fullBackupContent to specify an @xml resource which configures which files to backup.

我已按照此处的说明进行操作 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 文件中,我将用户注册 ID 存储在字符串 "registrationID" 下的共享首选项中。所以我假设我使用以下...

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

对吗?

所以我想通了。在我的 RegistrationIntentService.java 文件中,我将用户注册 ID 存储在字符串 "registrationID" 下的共享首选项中。所以在 backup_scheme.xml 文件中使用以下内容...

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

我遇到了同样的问题,我本来打算接受答案,但事实证明这似乎不是应该做的。

我所做的是在创建 backup_scheme.xml 文件之前在我的清单中设置 fullBackupContentProperty。所以当然,Android Studio 会发出警告,但为我提供了一个快速修复程序来自动生成该文件。

应用快速修复生成了以下文件:

<?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 的整个共享首选项文件,而不仅仅是密钥。

太糟糕了,文档没有说得很清楚,也太糟糕了,我们不得不从备份中排除整个文件。

根据@Simon 的回答进一步研究后,我认为当备份描述符 xml 文件是从 quickfix 自动生成时,我们仍然需要替换 Path 中指定的文件名Android工作室。我检查了我的一个应用程序中的 shared_prefs 目录,该目录已经实现了 Firebase 云消息传递,并找到了以下 GCM 设置首选项文件:

所以 backup_descriptor.xml 将是:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
    <!-- Exclude the shared preferences file that contains the GCM registrationId -->
    <exclude
        domain="sharedpref"
        path="com.google.android.gms.appid.xml"/>
    <exclude
        domain="sharedpref"
        path="com.google.android.gms.measurement.prefs.xml"/>
</full-backup-content>

要回答您的问题,您应该将文件指定为 path。 因此,对于 GCM regid,它是 sharedpref 域中的一个 XML,名为 com.google.android.gms.appid.xml

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

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

我遇到了同样的问题,写了一篇关于它的小文章。 https://medium.com/@dahlberg.bob/what-i-learned-from-lint-today-57c46b5f3225