Android 23+ - Exclude GCM Registration ID from backup
Asked Answered
F

4

23

I have an app which uses Azure to send Push notifications. Azure in turn, uses GCM to send to Android devices.

I'm noticing that I have a warning in my AndroidManifest.xml stating

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.

I have following the instructions here https://developer.android.com/training/backup/autosyncapi.html?hl=in#configuring

however I am stumped as to how to exclude the GCM regID from the backup? Here is my current setup.

Manifest

<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>

What do I put as path? Am I supposed to have a physical file somewhere I exclude?

UPDATE

So I think I figured it out. In my RegistrationIntentService.java file, I store the users registrationID in the shared preferences under the string "registrationID". So I'm assuming I use the following...

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

right?

Forster answered 22/8, 2016 at 20:23 Comment(1)
Under the Including or excluding data document and checking the information you provided, it should exclude registrationID in the backup. You should try it and if it works, post that as an answer for the benefit of the community.Hynes
F
5

So I figured it out. In my RegistrationIntentService.java file, I store the users registrationID in the shared preferences under the string "registrationID". So use the following in the backup_scheme.xml file...

<exclude domain="sharedpref" path="registrationID"/>
Forster answered 23/8, 2016 at 15:20 Comment(5)
the path element should be the entire .xml file, example: <exclude domain="sharedpref" path="user-preferences.xml" />Grassplot
Say, can you please explain what is this "registrationID" ? I got this warning, but I don't have any registration in my app. I just use some Google/Firebase services (ads,analytics,IAP...) ... What happens if I ignore this warning?Headlock
@androiddeveloper - registrationID is my internal variable I store my apps Firebase Cloud Messaging Registration ID in. It's stored in the SharedPrefs area on the device under the Key of "registrationID". You can ignore it safely I believe, it just means your device will re-register if the app is restored from backup.Forster
I don't understand. You mean it's something of your app, that isn't general and exists on others?Headlock
This is not the correct answer. @Turbojet has the right answer belowKordula
T
19

After further study from @Simon answer, I think we would still need to replace the file name specified in the Path when the backup descriptor xml file is auto-generated from the quickfix in Android Studio. I check the shared_prefs directory in one of my app which has implemented the Firebase Cloud Messaging and found the following GCM settings preferences files:

enter image description here

So the backup_descriptor.xml would be:

<?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>
Turbojet answered 7/8, 2018 at 3:20 Comment(3)
This is the right answer. Per the Android documentation, examples of files that should be excluded from backup: Files that have device specific identifiers, either issued by a server or generated on the device. For example, Google Cloud Messaging (GCM) needs to generate a registration token every time a user installs your app on a new device. If the old registration token is restored, the app may behave unexpectedly.Kordula
Honest, I wish this was in the "Getting Started" documentation. I was chasing a bug around FCM's SQLite DB then found this.Indigene
Wondering how they figured out these values? Open the Device File Explorer in Android Studio and find your app under the data folder. Look under the shared_prefs folder and you will find them.Varicose
D
7

I faced same issue and I was going to follow accepting answer, but turns out it seems it is not what should be done.

What I did is that I set the fullBackupContentProperty in my manifest before creating the backup_scheme.xml file. So of course, Android Studio complained with a warning but provided me a quickfix to automatically generate that file.

Applying the quickfix generated the following file:

<?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>

Note the <!-- Exclude the shared preferences file that contains the GCM registrationId -->

So my guess is that you have to exclude the entire Shared Preference file containing the GCM ID, and not just the key.

Too bad the documentation does not make it super clear and also too bad we have to exclude and entire file from the backup.

Decrial answered 11/1, 2018 at 0:52 Comment(1)
I think this should be the correct answer instead of the accepted one.Turbojet
F
5

So I figured it out. In my RegistrationIntentService.java file, I store the users registrationID in the shared preferences under the string "registrationID". So use the following in the backup_scheme.xml file...

<exclude domain="sharedpref" path="registrationID"/>
Forster answered 23/8, 2016 at 15:20 Comment(5)
the path element should be the entire .xml file, example: <exclude domain="sharedpref" path="user-preferences.xml" />Grassplot
Say, can you please explain what is this "registrationID" ? I got this warning, but I don't have any registration in my app. I just use some Google/Firebase services (ads,analytics,IAP...) ... What happens if I ignore this warning?Headlock
@androiddeveloper - registrationID is my internal variable I store my apps Firebase Cloud Messaging Registration ID in. It's stored in the SharedPrefs area on the device under the Key of "registrationID". You can ignore it safely I believe, it just means your device will re-register if the app is restored from backup.Forster
I don't understand. You mean it's something of your app, that isn't general and exists on others?Headlock
This is not the correct answer. @Turbojet has the right answer belowKordula
I
0

To answer your question you should specify the file as path. So for the GCM regid, it is an XML in the sharedpref domain called com.google.android.gms.appid.xml

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

But since that is not documented anywhere as I can find, it might change in the future without any notice. So a better approach is to specify only what you actually want to backup.

I had the same problem and wrote a small article on it. https://medium.com/@dahlberg.bob/what-i-learned-from-lint-today-57c46b5f3225

Inexpiable answered 31/8, 2020 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.