Skip to content

Commit 34ea6a4

Browse files
committed
GCM error status fix and misc
* Fixed rare issue where an out of date Google Play services app would unsubscribe the device when it may still be valid. * Added more subscription statuses for Google Play services initialization errors. * postNotification now allows setting app_id. * lockScreenVisibility default read value fix * Misc code clean up
1 parent 1502f0b commit 34ea6a4

File tree

10 files changed

+96
-84
lines changed

10 files changed

+96
-84
lines changed

OneSignalSDK/app/src/test/java/com/onesignal/ShadowPushRegistratorGPS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ShadowPushRegistratorGPS {
4343
private static PushRegistrator.RegisteredHandler lastCallback;
4444

4545
public static void manualFireRegisterForPush() {
46-
lastCallback.complete(regId);
46+
lastCallback.complete(regId, 1);
4747
}
4848

4949
public void registerForPush(Context context, String googleProjectNumber, PushRegistrator.RegisteredHandler callback) {
@@ -52,6 +52,6 @@ public void registerForPush(Context context, String googleProjectNumber, PushReg
5252
if (waitTimer > 0)
5353
SystemClock.sleep(waitTimer);
5454

55-
callback.complete(fail ? null : regId);
55+
callback.complete(fail ? null : regId, 1);
5656
}
5757
}

OneSignalSDK/app/src/test/java/com/test/onesignal/PushRegistratorRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testGooglePlayServicesAPKMissingOnDevice() throws Exception {
7474

7575
pushReg.registerForPush(blankActivity, "", new PushRegistrator.RegisteredHandler() {
7676
@Override
77-
public void complete(String id) {
77+
public void complete(String id, int status) {
7878
System.out.println("HERE: " + id);
7979
callbackFired = true;
8080
testThread.interrupt();
@@ -94,7 +94,7 @@ public void testGCMPartOfGooglePlayServicesMissing() throws Exception {
9494

9595
pushReg.registerForPush(blankActivity, "", new PushRegistrator.RegisteredHandler() {
9696
@Override
97-
public void complete(String id) {
97+
public void complete(String id, int status) {
9898
System.out.println("HERE: " + id);
9999
callbackFired = true;
100100
testThread.interrupt();

OneSignalSDK/onesignal/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ android {
3535
dependencies {
3636
provided fileTree(dir: 'libs', include: ['*.jar'])
3737

38-
compile 'com.google.android.gms:play-services-gcm:8.4.0'
39-
compile 'com.google.android.gms:play-services-location:8.4.0'
38+
compile 'com.google.android.gms:play-services-gcm:9.4.0'
39+
compile 'com.google.android.gms:play-services-location:9.4.0'
4040
}
4141

4242
// apply from: 'maven-push.gradle'

OneSignalSDK/onesignal/src/main/java/com/onesignal/GenerateNotification.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,10 @@ static void createSummaryNotification(Context inContext, boolean updateSummary,
435435
summeryBuilder.setTicker(summaryMessage);
436436

437437
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
438-
String line1Title = null;
439438

440439
// Add the latest notification to the summary
441440
if (!updateSummary) {
442-
line1Title = gcmBundle.optString("title", null);
441+
String line1Title = gcmBundle.optString("title", null);
443442

444443
if (line1Title == null)
445444
line1Title = "";

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSNotificationPayload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class OSNotificationPayload {
4343
public String launchUrl;
4444
public String sound;
4545
public String ledColor;
46-
public int lockScreenVisibility;
46+
public int lockScreenVisibility = 1;
4747
public String groupKey;
4848
public String groupMessage;
4949
public List<ActionButton> actionButtons;

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSUtils.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,69 @@
3737
import android.telephony.TelephonyManager;
3838

3939
import java.util.Locale;
40+
import java.util.UUID;
4041

4142
class OSUtils {
43+
44+
static final int UNINITIALIZABLE_STATUS = -999;
45+
46+
static int initializationChecker(int deviceType, String googleProjectNumber, String oneSignalAppId) {
47+
int subscribableStatus = 1;
48+
49+
try {
50+
//noinspection ResultOfMethodCallIgnored
51+
UUID.fromString(oneSignalAppId);
52+
} catch (Throwable t) {
53+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "OneSignal AppId format is invalid.\nExample: 'b2f7f966-d8cc-11e4-bed1-df8f05be55ba'\n", t);
54+
return UNINITIALIZABLE_STATUS;
55+
}
56+
57+
if ("b2f7f966-d8cc-11e4-bed1-df8f05be55ba".equals(oneSignalAppId) || "5eb5a37e-b458-11e3-ac11-000c2940e62c".equals(oneSignalAppId))
58+
OneSignal.Log(OneSignal.LOG_LEVEL.WARN, "OneSignal Example AppID detected, please update to your app's id found on OneSignal.com");
59+
60+
if (deviceType == 1) {
61+
try {
62+
//noinspection ResultOfMethodCallIgnored
63+
Double.parseDouble(googleProjectNumber);
64+
if (googleProjectNumber.length() < 8 || googleProjectNumber.length() > 16)
65+
throw new IllegalArgumentException("Google Project number (Sender_ID) should be a 10 to 14 digit number in length.");
66+
} catch (Throwable t) {
67+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "Google Project number (Sender_ID) format is invalid. Please use the 10 to 14 digit number found in the Google Developer Console for your project.\nExample: '703322744261'\n", t);
68+
subscribableStatus = -6;
69+
}
70+
71+
try {
72+
Class.forName("com.google.android.gms.gcm.GoogleCloudMessaging");
73+
} catch (ClassNotFoundException e) {
74+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The GCM Google Play services client library was not found. Please make sure to include it in your project.", e);
75+
subscribableStatus = -4;
76+
}
77+
78+
try {
79+
Class.forName("com.google.android.gms.common.GooglePlayServicesUtil");
80+
} catch (ClassNotFoundException e) {
81+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The GooglePlayServicesUtil class part of Google Play services client library was not found. Include this in your project.", e);
82+
subscribableStatus = -4;
83+
}
84+
}
85+
86+
try {
87+
Class.forName("android.support.v4.view.MenuCompat");
88+
try {
89+
Class.forName("android.support.v4.content.WakefulBroadcastReceiver");
90+
Class.forName("android.support.v4.app.NotificationManagerCompat");
91+
} catch (ClassNotFoundException e) {
92+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "The included Android Support Library v4 is to old or incomplete. Please update your project's android-support-v4.jar to the latest revision.", e);
93+
subscribableStatus = -5;
94+
}
95+
} catch (ClassNotFoundException e) {
96+
OneSignal.Log(OneSignal.LOG_LEVEL.FATAL, "Could not find the Android Support Library v4. Please make sure android-support-v4.jar has been correctly added to your project.", e);
97+
subscribableStatus = -3;
98+
}
99+
100+
return subscribableStatus;
101+
}
102+
42103
int getDeviceType() {
43104
try {
44105
// Class only available on the FireOS and only when the following is in the AndroidManifest.xml.

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.util.Iterator;
3838
import java.util.List;
3939
import java.util.TimeZone;
40-
import java.util.UUID;
4140

4241
import org.json.*;
4342

@@ -156,7 +155,7 @@ public void init() {
156155
private static LOG_LEVEL logCatLevel = LOG_LEVEL.WARN;
157156

158157
private static String userId = null;
159-
static int subscribableStatus = 1;
158+
private static int subscribableStatus;
160159

161160
static boolean initDone;
162161
private static boolean foreground;
@@ -226,64 +225,13 @@ public static void init(Context context, String googleProjectNumber, String oneS
226225
mInitBuilder = new OneSignal.Builder();
227226
mInitBuilder.mNotificationOpenedHandler = notificationOpenedHandler;
228227
mInitBuilder.mNotificationReceivedHandler = notificationReceivedHandler;
228+
mGoogleProjectNumber = googleProjectNumber;
229229

230230
osUtils = new OSUtils();
231-
232231
deviceType = osUtils.getDeviceType();
233-
234-
// START: Init validation
235-
try {
236-
//noinspection ResultOfMethodCallIgnored
237-
UUID.fromString(oneSignalAppId);
238-
} catch (Throwable t) {
239-
Log(LOG_LEVEL.FATAL, "OneSignal AppId format is invalid.\nExample: 'b2f7f966-d8cc-11e4-bed1-df8f05be55ba'\n", t);
232+
subscribableStatus = OSUtils.initializationChecker(deviceType, googleProjectNumber, oneSignalAppId);
233+
if (subscribableStatus == OSUtils.UNINITIALIZABLE_STATUS)
240234
return;
241-
}
242-
243-
if ("b2f7f966-d8cc-11e4-bed1-df8f05be55ba".equals(oneSignalAppId) || "5eb5a37e-b458-11e3-ac11-000c2940e62c".equals(oneSignalAppId))
244-
Log(LOG_LEVEL.WARN, "OneSignal Example AppID detected, please update to your app's id found on OneSignal.com");
245-
246-
if (deviceType == 1) {
247-
try {
248-
//noinspection ResultOfMethodCallIgnored
249-
Double.parseDouble(googleProjectNumber);
250-
if (googleProjectNumber.length() < 8 || googleProjectNumber.length() > 16)
251-
throw new IllegalArgumentException("Google Project number (Sender_ID) should be a 10 to 14 digit number in length.");
252-
} catch (Throwable t) {
253-
Log(LOG_LEVEL.FATAL, "Google Project number (Sender_ID) format is invalid. Please use the 10 to 14 digit number found in the Google Developer Console for your project.\nExample: '703322744261'\n", t);
254-
subscribableStatus = -6;
255-
}
256-
257-
try {
258-
Class.forName("com.google.android.gms.gcm.GoogleCloudMessaging");
259-
} catch (ClassNotFoundException e) {
260-
Log(LOG_LEVEL.FATAL, "The GCM Google Play services client library was not found. Please make sure to include it in your project.", e);
261-
subscribableStatus = -4;
262-
}
263-
264-
try {
265-
Class.forName("com.google.android.gms.common.GooglePlayServicesUtil");
266-
} catch (ClassNotFoundException e) {
267-
Log(LOG_LEVEL.FATAL, "The GooglePlayServicesUtil class part of Google Play services client library was not found. Include this in your project.", e);
268-
subscribableStatus = -4;
269-
}
270-
}
271-
272-
mGoogleProjectNumber = googleProjectNumber;
273-
274-
try {
275-
Class.forName("android.support.v4.view.MenuCompat");
276-
try {
277-
Class.forName("android.support.v4.content.WakefulBroadcastReceiver");
278-
Class.forName("android.support.v4.app.NotificationManagerCompat");
279-
} catch (ClassNotFoundException e) {
280-
Log(LOG_LEVEL.FATAL, "The included Android Support Library v4 is to old or incomplete. Please update your project's android-support-v4.jar to the latest revision.", e);
281-
subscribableStatus = -5;
282-
}
283-
} catch (ClassNotFoundException e) {
284-
Log(LOG_LEVEL.FATAL, "Could not find the Android Support Library v4. Please make sure android-support-v4.jar has been correctly added to your project.", e);
285-
subscribableStatus = -3;
286-
}
287235

288236
if (initDone) {
289237
if (context != null)
@@ -295,7 +243,6 @@ public static void init(Context context, String googleProjectNumber, String oneS
295243
return;
296244
}
297245

298-
// END: Init validation
299246
boolean contextIsActivity = (context instanceof Activity);
300247

301248
foreground = contextIsActivity;
@@ -371,7 +318,15 @@ private static void startRegistrationOrOnSession() {
371318

372319
pushRegistrator.registerForPush(appContext, mGoogleProjectNumber, new PushRegistrator.RegisteredHandler() {
373320
@Override
374-
public void complete(String id) {
321+
public void complete(String id, int status) {
322+
if (status < 1) {
323+
if (OneSignalStateSynchronizer.getRegistrationId() == null || subscribableStatus < -6)
324+
subscribableStatus = status;
325+
}
326+
// Allow the pushRegistrator to replace it's invalid status.
327+
else if (subscribableStatus < -6)
328+
subscribableStatus = status;
329+
375330
lastRegistrationId = id;
376331
registerForPushFired = true;
377332
registerUser();
@@ -746,7 +701,8 @@ public static void postNotification(String json, final PostNotificationResponseH
746701

747702
public static void postNotification(JSONObject json, final PostNotificationResponseHandler handler) {
748703
try {
749-
json.put("app_id", getSavedAppId());
704+
if (!json.has("app_id"))
705+
json.put("app_id", getSavedAppId());
750706

751707
OneSignalRestClient.post("notifications/", json, new OneSignalRestClient.ResponseHandler() {
752708
@Override
@@ -827,7 +783,7 @@ public void run() {
827783
}
828784

829785
public static void deleteTag(String key) {
830-
Collection<String> tempList = new ArrayList<String>(1);
786+
Collection<String> tempList = new ArrayList<>(1);
831787
tempList.add(key);
832788
deleteTags(tempList);
833789
}
@@ -1345,7 +1301,7 @@ static boolean isDuplicateNotification(String id, Context context) {
13451301
readableDb.close();
13461302

13471303
if (exists) {
1348-
Log(LOG_LEVEL.DEBUG, "Duplicate GCM message received, skipping processing. " + id);
1304+
Log(LOG_LEVEL.DEBUG, "Duplicate GCM message received, skip processing of " + id);
13491305
return true;
13501306
}
13511307

OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistrator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public interface PushRegistrator {
3333

3434
interface RegisteredHandler {
35-
void complete(String id);
35+
void complete(String id, int status);
3636
}
3737

3838
void registerForPush(Context context, String googleProjectNumber, RegisteredHandler callback);

OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorADM.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void run() {
4747
adm.startRegister();
4848
else {
4949
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "ADM Already registered with ID:" + registrationId);
50-
callback.complete(registrationId);
50+
callback.complete(registrationId, 1);
5151
}
5252

5353
try {
@@ -64,6 +64,6 @@ public void run() {
6464

6565
public static void fireCallback(String id) {
6666
callbackSuccessful = true;
67-
registeredCallback.complete(id);
67+
registeredCallback.complete(id, 1);
6868
}
6969
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorGPS.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import android.content.DialogInterface.OnClickListener;
4141
import android.content.pm.PackageInfo;
4242
import android.content.pm.PackageManager;
43-
import android.content.res.Resources;
4443

4544
import java.io.IOException;
4645

@@ -63,13 +62,11 @@ public void registerForPush(Context context, String googleProjectNumber, Registe
6362
registerInBackground(googleProjectNumber);
6463
else {
6564
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "No valid Google Play services APK found.");
66-
OneSignal.subscribableStatus = -7;
67-
registeredHandler.complete(null);
65+
registeredHandler.complete(null, -7);
6866
}
6967
} catch (Throwable t) {
7068
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Could not register with GCM due to an error with the AndroidManifest.xml file or with 'Google Play services'.", t);
71-
OneSignal.subscribableStatus = -8;
72-
registeredHandler.complete(null);
69+
registeredHandler.complete(null, -8);
7370
}
7471
}
7572

@@ -152,21 +149,20 @@ public void onClick(DialogInterface dialog, int which) {
152149
private void registerInBackground(final String googleProjectNumber) {
153150
new Thread(new Runnable() {
154151
public void run() {
155-
String registrationId = null;
156152
boolean firedComplete = false;
157153

158154
for (int currentRetry = 0; currentRetry < GCM_RETRY_COUNT; currentRetry++) {
159155
try {
160156
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(appContext);
161-
registrationId = gcm.register(googleProjectNumber);
157+
String registrationId = gcm.register(googleProjectNumber);
162158
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "Device registered, Google Registration ID = " + registrationId);
163-
registeredHandler.complete(registrationId);
159+
registeredHandler.complete(registrationId, 1);
164160
break;
165161
} catch (IOException e) {
166162
if (!"SERVICE_NOT_AVAILABLE".equals(e.getMessage())) {
167163
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error Getting Google Registration ID", e);
168164
if (!firedComplete)
169-
registeredHandler.complete(null);
165+
registeredHandler.complete(null, -11);
170166
break;
171167
}
172168
else {
@@ -176,15 +172,15 @@ public void run() {
176172
OneSignal.Log(OneSignal.LOG_LEVEL.INFO, "Google Play services returned SERVICE_NOT_AVAILABLE error. Current retry count: " + currentRetry, e);
177173
if (currentRetry == 2) {
178174
// Retry 3 times before firing a null response and continuing a few more times.
179-
registeredHandler.complete(null);
175+
registeredHandler.complete(null, -9);
180176
firedComplete = true;
181177
}
182178
try { Thread.sleep(10000 * (currentRetry + 1)); } catch (Throwable t) {}
183179
}
184180
}
185181
} catch (Throwable t) {
186182
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error Getting Google Registration ID", t);
187-
registeredHandler.complete(null);
183+
registeredHandler.complete(null, -12);
188184
break;
189185
}
190186
}

0 commit comments

Comments
 (0)