Skip to content

Commit 2c0b2d9

Browse files
committed
Add Main thread check under OSNotificationReceivedEvent
* Flutter invoked methods are runned under MainThread, OSNotificationReceivedEvent complete method ends calling a MainThread check that crashes. * Add MainThread check under OSNotificationReceivedEvent complete method * If running on MainThread create a new thread to complete the notification
1 parent 7cf40cc commit 2c0b2d9

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void run() {
6767
* @param notification can be null to omit displaying the notification,
6868
* or OSMutableNotification to modify the notification to display
6969
*/
70-
public synchronized void complete(@Nullable OSNotification notification) {
70+
public synchronized void complete(@Nullable final OSNotification notification) {
7171
timeoutHandler.destroyTimeout(timeoutRunnable);
7272

7373
if (isComplete) {
@@ -77,6 +77,20 @@ public synchronized void complete(@Nullable OSNotification notification) {
7777

7878
isComplete = true;
7979

80+
if (OSUtils.isRunningOnMainThread()) {
81+
new Thread(new Runnable() {
82+
@Override
83+
public void run() {
84+
processNotification(notification);
85+
}
86+
}, "OS_COMPLETE_NOTIFICATION").start();
87+
return;
88+
}
89+
90+
processNotification(notification);
91+
}
92+
93+
private void processNotification(@Nullable OSNotification notification) {
8094
// Pass copies to controller, to avoid modifying objects accessed by the user
8195
controller.processNotification(this.notification.copy(), notification != null ? notification.copy() : null);
8296
}

OneSignalSDK/unittest/src/test/java/com/onesignal/OneSignalPackagePrivateHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Set;
2323
import java.util.concurrent.ConcurrentLinkedQueue;
2424

25+
import static com.test.onesignal.TestHelpers.threadAndTaskWait;
2526
import static org.robolectric.Shadows.shadowOf;
2627

2728
public class OneSignalPackagePrivateHelper {
@@ -142,12 +143,13 @@ public static void FCMBroadcastReceiver_onReceived_withIntent(Context context, I
142143
receiver.onReceive(context, intent);
143144
}
144145

145-
public static void FCMBroadcastReceiver_onReceived_withBundle(Context context, Bundle bundle) {
146+
public static void FCMBroadcastReceiver_onReceived_withBundle(Context context, Bundle bundle) throws Exception {
146147
FCMBroadcastReceiver receiver = new FCMBroadcastReceiver();
147148
Intent intent = new Intent();
148149
intent.setAction("com.google.android.c2dm.intent.RECEIVE");
149150
intent.putExtras(bundle);
150151
receiver.onReceive(context,intent);
152+
threadAndTaskWait();
151153
}
152154

153155
public static void HMSProcessor_processDataMessageReceived(final Context context, final String jsonStrPayload) {

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,14 +1129,15 @@ public void shouldNotSetAlertnessFieldsOnLowPriority() throws Exception {
11291129

11301130
@Test
11311131
@Config(shadows = { ShadowGenerateNotification.class })
1132-
public void shouldSetExpireTimeCorrectlyFromGoogleTTL() {
1132+
public void shouldSetExpireTimeCorrectlyFromGoogleTTL() throws Exception {
11331133
long sentTime = 1_553_035_338_000L;
11341134
long ttl = 60L;
11351135

11361136
Bundle bundle = getBaseNotifBundle();
11371137
bundle.putLong("google.sent_time", sentTime);
11381138
bundle.putLong("google.ttl", ttl);
11391139
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
1140+
threadAndTaskWait();
11401141

11411142
HashMap<String, Object> notification = TestHelpers.getAllNotificationRecords(dbHelper).get(0);
11421143
long expireTime = (Long)notification.get(NotificationTable.COLUMN_NAME_EXPIRE_TIME);
@@ -1145,8 +1146,9 @@ public void shouldSetExpireTimeCorrectlyFromGoogleTTL() {
11451146

11461147
@Test
11471148
@Config(shadows = { ShadowGenerateNotification.class })
1148-
public void shouldSetExpireTimeCorrectlyWhenMissingFromPayload() {
1149+
public void shouldSetExpireTimeCorrectlyWhenMissingFromPayload() throws Exception {
11491150
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, getBaseNotifBundle());
1151+
threadAndTaskWait();
11501152

11511153
long expireTime = (Long)TestHelpers.getAllNotificationRecords(dbHelper).get(0).get(NotificationTable.COLUMN_NAME_EXPIRE_TIME);
11521154
assertEquals((SystemClock.currentThreadTimeMillis() / 1_000L) + 259_200, expireTime);

OneSignalSDK/unittest/src/test/java/com/test/onesignal/OutcomeEventIntegrationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ public void testIndirectSession_fromDirectSession_afterNewSession() throws Excep
694694
// Receive notification
695695
Bundle bundle = getBaseNotifBundle(ONESIGNAL_NOTIFICATION_ID + "2");
696696
FCMBroadcastReceiver_onReceived_withBundle(blankActivity, bundle);
697-
threadAndTaskWait();
698697

699698
// Wait 31 seconds
700699
time.advanceSystemTimeBy(31);

OneSignalSDK/unittest/src/test/java/com/test/onesignal/TestHelpers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static Thread getThreadByName(String threadName) {
201201
}
202202

203203
// Run any OneSignal background threads including any pending runnables
204-
static void threadAndTaskWait() throws Exception {
204+
public static void threadAndTaskWait() throws Exception {
205205
ShadowApplication.getInstance().getForegroundThreadScheduler().runOneTask();
206206
// Runs Runnables posted by calling View.post() which are run on the main thread.
207207
Robolectric.getForegroundThreadScheduler().runOneTask();

0 commit comments

Comments
 (0)