Android GCM - same registration id each time on different devices - android

I am trying to use GCM and everything is working fine except for the GCMRegistrar.getRegistrationId call on difference devices. I've run this code on an emulator and 2 different phones and GCMRegistrar.getRegistrationId always returns the same string. I expected it to differ on each device but it does not. Am I doing something wrong? Here's the code I'm using (taken from the docs).
GCMRegistrar.checkDevice(context);
final String regId = GCMRegistrar.getRegistrationId(context);
if (regId.equals("")) {
GCMRegistrar.register(activity, "123456789012"); //sender id/api project id
} else {
LogUtil.info("Already registered");
}
When I first ran this on the emulator GCMRegistrar.getRegistrationId returned "" but on subsequent calls it returns the big id. That makes sense. The strange thing was that when running the same code on the 2 phones, they both returned that same id (the one the emulator returned), having never run this app before.

From what I know, the registration ID is used to match a user+device. On emulators, you don't have any user account, and the device is virtual so my guess is that the registration thinks both virtual devices are the same.
I don't know what they use to create the registration ID, but if they use the ANDROID_ID, it's a known problem that all virtual devices have the same ID.
Also it's a known problem that some manufacturers use the same device ID for all their devices although it is supposed to be unique. But maybe they are using something else. Maybe you should try to ask GCM support for this particular issue.

This actually turned out to be a mistake on my part on how I was checking the id's. XGouchet, thanks for your response though.

Related

Maintain single device for registering account of my app

I am designing a chat application with registered phone number for both iOS and android app.
I want to make sure that this number and app works on one phone at a time, as below scenarios given I want to make sure that app once authenticated to another device with same account details disable the previous device.
1) app installed on one iOS device registered account and deleted and reinstalled on same device again
2) app installed on iOS phone then same account is validated on android app the iOS phone app should show disabled account
3) app installed on two android phone with same number should disable old one automatically.
Reason is I don't want multiple copies of application with same number running to avoid like whatsapp does.
I am thinking of device keychain for iOS and android Mac Id usage to get this worked out since apple stopped UDID broadcasting to server.
Also have a vague Idea about this vendor id apple providing.
Can anyone advice on this. How can I achieve so app is running with same account on one phone only and other just stops.
If you are binding an app with a phone number getting registered then it can run only on one device since you can't have same ph number running on 1+ devices at the same time. From user's perspective if he is changing same number between multiple handsets, going by your case, this app is blocking previous device of user, it sounds like a never ending loop. Everytime user's previous device is getting blocked or it may result in blocking all user's device. Dosen't make sense to me at least.
Below method is for getting device ID
public static String getDeviceID(Context p_context) throws Throwable
{
String m_deviceID = null;
TelephonyManager m_telephonyManager = null;
m_telephonyManager = (TelephonyManager) p_context
.getSystemService(Context.TELEPHONY_SERVICE);
m_deviceID = m_telephonyManager.getDeviceId().toString();
if (m_deviceID == null || "00000000000000".equalsIgnoreCase(m_deviceID))
{
m_deviceID = "AAAAAAA";
}
return m_deviceID;
}
More information about the identification you can read here.

IMEI Number on non-network devices

I'm currently working on an application where the code uses the device IMEI number as a variable to store something in the DB. We need to see what device the reading originated from.
ie:
String IMEI = telephonyManager.getDeviceId();
However, I'm not sure how this will behave on phones without network access, such as a wifi only tablet. Will is return a unique value? Will it return null?
I know there are alternatives to using IMEI, and I don't need any explanation on what alternative values I can use. I only need to know what will happen when I call this on a phone with no network access. Please do not provide suggestions on alternative IDs or anything other than what I have asked. I appreciate you taking the time to read this question.
Thanks,
-Mark
You get null.
Just ran into this on an app I wrote 3 years ago, and only just converted to tablet format. I had forgotten we were using IMEI (against the advice you and I both received ;-) ) and we started sending a whole bunch of null device IDs to the server.
Sadly it took a whole lot of network logs before I tracked it down to that little hack.

What is difference between android device id and device token?

I am getting android device id from this code of snippet which is for my device is "3b3472d8998af818"
protected String getDeviceId() {
return Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
}
and android device token from this code of snippet which is for my device is APA91bGeA3xOsXocz-eNOklONNVYwHyjvzyVMPQtC54_GX5Npx5fjWjpDbw6XOGqFi-a0lz7gL4BQlZXN-opPyHwJxxo3-1jyqjc1df6y8KwdVj7tUHVObcE3sF0XSpSngUkq6UfCEUTwgmsv-sjGuK863Y4R1kmHA
GCMRegistrar.register(this.context, CommonUtilities.SENDER_ID);
I don't know what is the difference between two ids?
Both are completely different.
Secure.ANDROID_ID
A 64-bit number (as a hex string) that is randomly generated on the
device's first boot and should remain constant for the lifetime of the
device. (The value may change if a factory reset is performed on the
device.)
And GCMRegistrar.register(this.context, CommonUtilities.SENDER_ID); Gives you the registartion id of device from GCM. By which, GCM uniquely identifies the device.
Another difference is Secure.ANDROID_ID may change when factory reset is performed on the device. And Registration id gets changes periodically.
And if you are asking about CommonUtilities.SENDER_ID then This is your project number, and it will be used later on as the GCM sender ID

Where and when is generated android.Build.SERIAL in AOSP?

I know, that android.Build.SERIAL is generated at first device boot, but I can't locate where and when exactly. I'm building AOSP Jelly Bean, Android tablet, nosdcard.
2nd question: is this serial number really unique for all Android devices?
According to this thread, it clearly says that it's unique, but added since API 9 and may be not present on all devices.
If you're writing your app for a specific device's model, you could direclty check if it has an IMEI. Otherwise, as you said, I recommend you to write a custom ID generator module. You will be sure that your ID will be unique and available for all devices.
IMEI represents the serial number of the device. It's sure it's unique. Two different devices can't have the same serial number.
To get the serial number of the device you just have to call :
String serial = Build.SERIAL;
It exists another approach. You can get the id by calling Secure.ANDROID_ID.
A 64-bit number (as a hex string) that is randomly generated on the
device's first boot and should remain constant for the lifetime of the
device. (The value may change if a factory reset is performed on the
device.)
private final String ANDROID_ID = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
Take care because it says that the value MAY change if a factory reset is performed.

Is android.os.Build.SERIAL unique?

I would like to use a unique id for android device that works for phone and Tablet.
(IMEI doesn't work with no SIM card device and sometime MAC Address return null)
I'm not sure is android.os.Build.SERIAL unique or not.
Does anyone know about this?
Thanks,
Regards.
Yes, but note that it was only added in API level 9, and it may not be present on all devices. To get a unique ID on earlier platforms, you'll need to read something like the MAC address or IMEI.
Generally, try reading all the possible IDs, and use whichever are available. See this article for guidance.
You can use Build serial and android ID to make your own unique id.
String serial = Build.SERIAL;
String android_id =Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
String myKey=serial +android_id ;
Serial was only exposed in API:9. but you can get it in older versions using reflection. However the docs mention "if available" so I guess don't rely on it.
String deviceSerial = (String) Build.class.getField("SERIAL").get(
null);
I think for unique Id you should used android Id.
following is code to get Android Id.
String android_id = Secure.getString(this.getContentResolver(),
Secure.ANDROID_ID);
Log.d("Android","Android ID : "+android_id);
If it is there, then it is expected to be unique. But there is no guarantee that this property is set. Also it is API 9. Unfortunately there's no easy way to uniquely identify the device. Some properties like said SERIAL can be present, others like ANDROID_ID as NOT unique, some other like MAC depends on presence of WIFI or its state (if wifi module is off, the you may not be able to read its MAC). Some like IMEI cannot be read even device got phone module. So the best approach is to gather as much data as you can, and try to build something you could most likely consider unique device ID
What about a combination of MAC, IMEI and SERIAL ?
You just have to deal with the fact that they all could be non existant esp. on older phones without SIM.
I just find it odd that MAC would return null. This should not be possible imho, as it makes no sense that a mobile device has no MAC.
There are 2 MAC addresses possible but they can be non accessible in some cases.

Categories

Resources