3737import android .content .Context ;
3838import android .os .AsyncTask ;
3939import android .os .Build ;
40+ import android .provider .Settings ;
4041import com .backendless .async .callback .AsyncCallback ;
4142import com .backendless .exceptions .BackendlessException ;
4243import com .backendless .exceptions .BackendlessFault ;
6162
6263public final class Messaging
6364{
64- public static String DEVICE_ID ;
6565 private final static String MESSAGING_MANAGER_SERVER_ALIAS = "com.backendless.services.messaging.MessagingService" ;
6666 private final static String DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS = "com.backendless.services.messaging.DeviceRegistrationService" ;
6767 private final static String EMAIL_MANAGER_SERVER_ALIAS = "com.backendless.services.mail.CustomersEmailService" ;
@@ -80,13 +80,8 @@ private Messaging()
8080
8181 static
8282 {
83- String id = null ;
8483 if ( Backendless .isAndroid () )
8584 {
86- if ( android .os .Build .VERSION .SDK_INT < 26 )
87- id = Build .SERIAL ;
88- else
89- id = Build .getSerial ();
9085 OS_VERSION = String .valueOf ( Build .VERSION .SDK_INT );
9186 OS = "ANDROID" ;
9287 }
@@ -95,8 +90,22 @@ private Messaging()
9590 OS_VERSION = System .getProperty ( "os.version" );
9691 OS = System .getProperty ( "os.name" );
9792 }
93+ }
94+
95+ static class DeviceIdHolder
96+ {
97+ static String id ;
9898
99- if ( id == null || id .equals ( "" ) )
99+ static void init ( Context context )
100+ {
101+ if ( android .os .Build .VERSION .SDK_INT < 27 )
102+ id = Build .SERIAL ;
103+ else
104+ id = Settings .Secure .getString ( context .getContentResolver (), Settings .Secure .ANDROID_ID );
105+ }
106+
107+ static void init ()
108+ {
100109 try
101110 {
102111 id = UUID .randomUUID ().toString ();
@@ -111,10 +120,15 @@ private Messaging()
111120 builder .append ( System .getProperty ( "java.home" ) );
112121 id = UUID .nameUUIDFromBytes ( builder .toString ().getBytes () ).toString ();
113122 }
123+ }
124+ }
114125
115- DEVICE_ID = id ;
126+ public static String getDeviceId ()
127+ {
128+ return DeviceIdHolder .id ;
116129 }
117130
131+
118132 static Messaging getInstance ()
119133 {
120134 return instance ;
@@ -214,7 +228,7 @@ public String registerDeviceOnServer( String deviceToken, final List<String> cha
214228 throw new IllegalArgumentException ( ExceptionMessage .NULL_DEVICE_TOKEN );
215229
216230 DeviceRegistration deviceRegistration = new DeviceRegistration ();
217- deviceRegistration .setDeviceId ( DEVICE_ID );
231+ deviceRegistration .setDeviceId ( getDeviceId () );
218232 deviceRegistration .setOs ( OS );
219233 deviceRegistration .setOsVersion ( OS_VERSION );
220234 deviceRegistration .setDeviceToken ( deviceToken );
@@ -234,7 +248,7 @@ public void registerDeviceOnServer( String deviceToken, final List<String> chann
234248 throw new IllegalArgumentException ( ExceptionMessage .NULL_DEVICE_TOKEN );
235249
236250 DeviceRegistration deviceRegistration = new DeviceRegistration ();
237- deviceRegistration .setDeviceId ( DEVICE_ID );
251+ deviceRegistration .setDeviceId ( getDeviceId () );
238252 deviceRegistration .setOs ( OS );
239253 deviceRegistration .setOsVersion ( OS_VERSION );
240254 deviceRegistration .setDeviceToken ( deviceToken );
@@ -315,12 +329,12 @@ protected void onPostExecute( RuntimeException result )
315329
316330 public boolean unregisterDeviceOnServer () throws BackendlessException
317331 {
318- return (Boolean ) Invoker .invokeSync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "unregisterDevice" , new Object [] { DEVICE_ID } );
332+ return (Boolean ) Invoker .invokeSync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "unregisterDevice" , new Object [] { getDeviceId () } );
319333 }
320334
321335 public void unregisterDeviceOnServer ( final AsyncCallback <Boolean > responder )
322336 {
323- Invoker .invokeAsync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "unregisterDevice" , new Object [] { DEVICE_ID }, responder );
337+ Invoker .invokeAsync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "unregisterDevice" , new Object [] { getDeviceId () }, responder );
324338 }
325339
326340 public DeviceRegistration getDeviceRegistration () throws BackendlessException
@@ -330,7 +344,7 @@ public DeviceRegistration getDeviceRegistration() throws BackendlessException
330344
331345 public DeviceRegistration getRegistrations () throws BackendlessException
332346 {
333- return (DeviceRegistration ) Invoker .invokeSync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "getDeviceRegistrationByDeviceId" , new Object [] { DEVICE_ID } );
347+ return (DeviceRegistration ) Invoker .invokeSync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "getDeviceRegistrationByDeviceId" , new Object [] { getDeviceId () } );
334348 }
335349
336350 public void getDeviceRegistration ( AsyncCallback <DeviceRegistration > responder )
@@ -340,7 +354,7 @@ public void getDeviceRegistration( AsyncCallback<DeviceRegistration> responder )
340354
341355 public void getRegistrations ( AsyncCallback <DeviceRegistration > responder )
342356 {
343- Invoker .invokeAsync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "getDeviceRegistrationByDeviceId" , new Object [] { DEVICE_ID }, responder );
357+ Invoker .invokeAsync ( DEVICE_REGISTRATION_MANAGER_SERVER_ALIAS , "getDeviceRegistrationByDeviceId" , new Object [] { getDeviceId () }, responder );
344358 }
345359
346360 /**
0 commit comments