Skip to content

Commit 24a7c17

Browse files
milos1290e7mac
authored andcommitted
E2-1930 Fixing a crash loop when receiving push notification (#366)
1 parent ffd6f15 commit 24a7c17

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/RequestOld.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,15 @@ public Map<String, Object> createArgsDictionary() {
274274

275275
private void saveRequestForLater(Map<String, Object> args) {
276276
try {
277+
278+
Context context = Leanplum.getContext();
279+
if (context == null) {
280+
return;
281+
}
282+
277283
requestSequenceRecorder.beforeWrite();
278284

279285
synchronized (RequestOld.class) {
280-
Context context = Leanplum.getContext();
281286
SharedPreferences preferences = context.getSharedPreferences(
282287
LEANPLUM, Context.MODE_PRIVATE);
283288
SharedPreferences.Editor editor = preferences.edit();

AndroidSDKFcm/src/main/java/com/leanplum/LeanplumPushFirebaseMessagingService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public void onNewToken(String token) {
6161
@Override
6262
public void onMessageReceived(RemoteMessage remoteMessage) {
6363
try {
64+
if (Leanplum.getContext() == null) {
65+
Leanplum.setApplicationContext(this);
66+
}
6467
Map<String, String> messageMap = remoteMessage.getData();
6568
if (messageMap.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
6669
LeanplumPushService.handleNotification(this, getBundle(messageMap));

AndroidSDKTests/src/test/java/com/leanplum/internal/RequestOldTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.leanplum.internal;
2222

2323
import android.app.Application;
24+
import android.content.Context;
2425

2526
import com.leanplum.Leanplum;
2627
import com.leanplum.__setup.LeanplumTestApp;
@@ -371,6 +372,34 @@ private List<Map<String, Object>> mockRequests(int requestSize) {
371372
return requests;
372373
}
373374

375+
@Test
376+
public void testNotSendingIfContextIsNull() {
377+
Context context = Leanplum.getContext();
378+
Leanplum.setApplicationContext(null);
379+
380+
final Semaphore semaphore = new Semaphore(1);
381+
semaphore.tryAcquire();
382+
383+
// Given a request.
384+
Map<String, Object> params = new HashMap<>();
385+
params.put("data1", "value1");
386+
params.put("data2", "value2");
387+
RequestOld request = new RequestOld(POST, Constants.Methods.START, params);
388+
request.onError(new RequestOld.ErrorCallback() {
389+
@Override
390+
public void error(Exception e) {
391+
assertNotNull(e);
392+
semaphore.release();
393+
}
394+
});
395+
request.setAppId("fskadfshdbfa", "wee5w4waer422323");
396+
397+
// When the request is sent.
398+
request.sendIfConnected();
399+
400+
Leanplum.setApplicationContext(context);
401+
}
402+
374403
private static class ThreadRequestSequenceRecorder implements RequestSequenceRecorder {
375404
Instant beforeReadTime, afterReadTime, beforeWriteTime, afterWriteTime;
376405
final Semaphore writeSemaphore = new Semaphore(0);

0 commit comments

Comments
 (0)