Skip to content

Commit 01d9360

Browse files
committed
fixed BKNDLSS-17002 Anroid 8 (Oreo) devices not working with real time library
1 parent d4383c6 commit 01d9360

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/com/backendless/Backendless.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ public static void initApp( Object context, final String applicationId, final St
172172
Context appContext = ( (Context) context ).getApplicationContext();
173173
UserTokenStorageFactory.instance().init( appContext );
174174
UserIdStorageFactory.instance().init( appContext );
175+
com.backendless.Messaging.DeviceIdHolder.init( appContext );
176+
}
177+
else
178+
{
179+
com.backendless.Messaging.DeviceIdHolder.init( );
175180
}
176181

177182
if( isCodeRunner() )

src/com/backendless/Messaging.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.content.Context;
3838
import android.os.AsyncTask;
3939
import android.os.Build;
40+
import android.provider.Settings;
4041
import com.backendless.async.callback.AsyncCallback;
4142
import com.backendless.exceptions.BackendlessException;
4243
import com.backendless.exceptions.BackendlessFault;
@@ -61,7 +62,6 @@
6162

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

Comments
 (0)