The Cordova Persistent ID Plugin provides a robust solution for generating and maintaining a persistent unique identifier for your Cordova/Ionic applications on Android devices. This plugin is designed to create an ID that persists across app uninstalls, reinstalls, and version upgrades.
- Generates a unique identifier that persists across app lifecycles
- Uses multiple fallback methods to ensure consistency
- Handles Android runtime permissions automatically
- Compatible with Cordova and Ionic frameworks
To install this plugin, run the following command in your Cordova project:
cordova plugin add cordova-plugin-persistentid
For Ionic projects, use:
ionic cordova plugin add cordova-plugin-persistentid
Here's how to use the plugin in your TypeScript or JavaScript code:
declare let persistentId: any;
// ...
persistentId.getUniqueId(
(id: string) => {
console.log('Unique ID:', id);
// Use the ID as needed
},
(error: any) => {
console.error('Error getting unique ID:', error);
}
);
For Ionic applications, you might use it in a service like this:
import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class UniqueIdService {
constructor(private platform: Platform) {}
async getUniqueId(): Promise<string> {
return new Promise((resolve, reject) => {
this.platform.ready().then(() => {
persistentId.getUniqueId(resolve, reject);
});
});
}
}
Then use it in your components:
constructor(private uniqueIdService: UniqueIdService) {}
async ngOnInit() {
try {
const uniqueId = await this.uniqueIdService.getUniqueId();
console.log('Unique ID:', uniqueId);
} catch (error) {
console.error('Error getting unique ID:', error);
}
}
The plugin uses a multi-step approach to generate and retrieve a persistent ID:
- First, it checks for an existing ID in the app's SharedPreferences.
- If not found, it attempts to use the device's serial number (requires permission).
- If the serial number is unavailable, it falls back to using the Android ID.
- As a last resort, it generates a new UUID.
The retrieved or generated ID is then stored in SharedPreferences for future use.
This plugin requires the READ_PHONE_STATE
permission on Android to access the device's serial number. The permission is automatically added to your AndroidManifest.xml
file, but you need to request it at runtime for Android 6.0 (API level 23) and above.
- Persistence: While this plugin aims to provide a persistent ID, it's not 100% guaranteed to survive all scenarios (e.g., factory resets).
- Permissions: Users may deny the
READ_PHONE_STATE
permission, in which case the plugin falls back to other methods. - Android 10+: Access to the serial number is restricted on Android 10 (API level 29) and above, even with permissions.
- Privacy: Collecting and storing persistent identifiers may have privacy implications. Ensure your app's privacy policy adequately covers this.
If you're experiencing issues:
- Ensure the plugin is properly installed.
- Check that you've requested and been granted the necessary permissions.
- Look for any error messages in the console or plugin callbacks.
- On Android 10+, be aware that access to the serial number might be restricted.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Remember to update your app's privacy policy to reflect the use of this plugin and the data it collects.