Skip to content

Commit c28ed78

Browse files
committed
Fix Android 5.1 and older not resuming app
App would not resume on Android 5 after the `taskAffinity=""` addition added in the last commit. This seems to be an issue with allowTaskReparenting not working in this case. Even explicitly enabling it did not work. `taskAffinity` is required for newer vesions of Android to correctly start a new task and to reparent it to the correct task. This is why two different implementations had to be added.
1 parent eaabbe2 commit c28ed78

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

OneSignalSDK/onesignal/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@
140140
android:theme="@android:style/Theme.Translucent.NoTitleBar"
141141
android:exported="true" />
142142

143+
<activity
144+
android:name="com.onesignal.NotificationOpenedReceiverAndroid22AndOlder"
145+
android:noHistory="true"
146+
android:excludeFromRecents="true"
147+
android:theme="@android:style/Theme.Translucent.NoTitleBar"
148+
android:exported="true" />
143149
</application>
144150

145151
<!-- NOTE: See release version for tags with placeholders -->

OneSignalSDK/onesignal/src/main/java/com/onesignal/GenerateNotificationOpenIntent.kt

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,56 @@ class GenerateNotificationOpenIntent(
1010
private val startApp: Boolean
1111
) {
1212

13-
private val notificationOpenedClass: Class<*> = NotificationOpenedReceiver::class.java
13+
private val notificationOpenedClassAndroid23Plus: Class<*> = NotificationOpenedReceiver::class.java
14+
private val notificationOpenedClassAndroid22AndOlder: Class<*> = NotificationOpenedReceiverAndroid22AndOlder::class.java
1415

1516
fun getNewBaseIntent(
1617
notificationId: Int,
1718
): Intent {
19+
val intent =
20+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
21+
getNewBaseIntentAndroidAPI23Plus()
22+
else
23+
getNewBaseIntentAndroidAPI22AndOlder()
24+
25+
return intent
26+
.putExtra(
27+
GenerateNotification.BUNDLE_KEY_ANDROID_NOTIFICATION_ID,
28+
notificationId
29+
)
1830
// We use SINGLE_TOP and CLEAR_TOP as we don't want more than one OneSignal invisible click
1931
// tracking Activity instance around.
20-
val intentFlags =
32+
.addFlags(
2133
Intent.FLAG_ACTIVITY_SINGLE_TOP or
2234
Intent.FLAG_ACTIVITY_CLEAR_TOP
35+
)
36+
}
2337

38+
private fun getNewBaseIntentAndroidAPI23Plus(): Intent {
2439
return Intent(
2540
context,
26-
notificationOpenedClass
41+
notificationOpenedClassAndroid23Plus
2742
)
28-
.putExtra(
29-
GenerateNotification.BUNDLE_KEY_ANDROID_NOTIFICATION_ID,
30-
notificationId
43+
}
44+
45+
// See NotificationOpenedReceiverAndroid22AndOlder.java for details
46+
private fun getNewBaseIntentAndroidAPI22AndOlder(): Intent {
47+
val intent = Intent(
48+
context,
49+
notificationOpenedClassAndroid22AndOlder
3150
)
32-
.addFlags(intentFlags)
51+
52+
if (getIntentVisible() == null) {
53+
// If we don't show a visible Activity put OneSignal's invisible click tracking
54+
// Activity on it's own task so it doesn't resume an existing one once it closes.
55+
intent.addFlags(
56+
Intent.FLAG_ACTIVITY_NEW_TASK or
57+
Intent.FLAG_ACTIVITY_MULTIPLE_TASK or
58+
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
59+
)
60+
}
61+
62+
return intent
3363
}
3464

3565
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2021 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal;
29+
30+
import android.app.Activity;
31+
import android.content.Intent;
32+
import android.os.Bundle;
33+
34+
/**
35+
* This is the same as NotificationOpenedReceiver expect it doesn't contain
36+
* android:taskAffinity="" in it's AndroidManifest.xml entry. This is to
37+
* account for the resume behavior not working on Android API 22 and older.
38+
*
39+
* In Android 5.x and older, android:taskAffinity="" starts a new task as
40+
* expected, however the allowTaskReparenting behavior does not happen which
41+
* results in the app not resuming, when using reverse Activity trampoline.
42+
* Oddly enough cold starts of the app were not a problem.
43+
*/
44+
public class NotificationOpenedReceiverAndroid22AndOlder extends Activity {
45+
46+
@Override
47+
protected void onCreate(Bundle savedInstanceState) {
48+
super.onCreate(savedInstanceState);
49+
NotificationOpenedProcessor.processFromContext(this, getIntent());
50+
finish();
51+
}
52+
53+
@Override
54+
protected void onNewIntent(Intent intent) {
55+
super.onNewIntent(intent);
56+
NotificationOpenedProcessor.processFromContext(this, getIntent());
57+
finish();
58+
}
59+
60+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ private void generateNotificationWithLaunchURL() throws Exception {
11801180
}
11811181

11821182
private void assertNotificationOpenedReceiver(@NonNull Intent intent) {
1183-
assertEquals("com.onesignal.NotificationOpenedReceiver", intent.getComponent().getClassName());
1183+
assertEquals(com.onesignal.NotificationOpenedReceiverAndroid22AndOlder.class.getName(), intent.getComponent().getClassName());
11841184
}
11851185

11861186
private void assertOpenMainActivityIntent(@NonNull Intent intent) {

0 commit comments

Comments
 (0)