Skip to content

Commit 4c01f3e

Browse files
committed
Separate notification generate from open logic
GenerateNotificationOpenIntent used to handle everything related to Intents for notifications since it had to create the full reverse Activity trampoline and attach it to the notification. Now the notification generation logic simply needs to always open the SDK's invisible click tracking Activity. When the notification is open the runtime logic will take the push payload into consideration to trampoline to the destination Activity.
1 parent 324f5e9 commit 4c01f3e

File tree

3 files changed

+86
-87
lines changed

3 files changed

+86
-87
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,8 @@ private static boolean showNotification(OSNotificationGenerationJob notification
275275
JSONObject fcmJson = notificationJob.getJsonPayload();
276276
String group = fcmJson.optString("grp", null);
277277

278-
GenerateNotificationOpenIntent intentGenerator = GenerateNotificationOpenIntentFromPushPayload.INSTANCE.create(
279-
currentContext,
280-
fcmJson
278+
IntentGeneratorForAttachingToNotifications intentGenerator = new IntentGeneratorForAttachingToNotifications(
279+
currentContext
281280
);
282281

283282
ArrayList<StatusBarNotification> grouplessNotifs = new ArrayList<>();
@@ -368,7 +367,7 @@ private static boolean showNotification(OSNotificationGenerationJob notification
368367

369368
private static Notification createGenericPendingIntentsForNotif(
370369
NotificationCompat.Builder notifBuilder,
371-
GenerateNotificationOpenIntent intentGenerator,
370+
IntentGeneratorForAttachingToNotifications intentGenerator,
372371
JSONObject gcmBundle,
373372
int notificationId
374373
) {
@@ -385,7 +384,7 @@ private static Notification createGenericPendingIntentsForNotif(
385384

386385
private static void createGenericPendingIntentsForGroup(
387386
NotificationCompat.Builder notifBuilder,
388-
GenerateNotificationOpenIntent intentGenerator,
387+
IntentGeneratorForAttachingToNotifications intentGenerator,
389388
JSONObject gcmBundle,
390389
String group,
391390
int notificationId
@@ -495,9 +494,8 @@ static void updateSummaryNotification(OSNotificationGenerationJob notificationJo
495494
private static void createSummaryNotification(OSNotificationGenerationJob notificationJob, OneSignalNotificationBuilder notifBuilder) {
496495
boolean updateSummary = notificationJob.isRestoring();
497496
JSONObject fcmJson = notificationJob.getJsonPayload();
498-
GenerateNotificationOpenIntent intentGenerator = GenerateNotificationOpenIntentFromPushPayload.INSTANCE.create(
499-
currentContext,
500-
fcmJson
497+
IntentGeneratorForAttachingToNotifications intentGenerator = new IntentGeneratorForAttachingToNotifications(
498+
currentContext
501499
);
502500

503501
String group = fcmJson.optString("grp", null);
@@ -706,7 +704,7 @@ private static void createSummaryNotification(OSNotificationGenerationJob notifi
706704
@RequiresApi(api = Build.VERSION_CODES.M)
707705
private static void createGrouplessSummaryNotification(
708706
OSNotificationGenerationJob notificationJob,
709-
GenerateNotificationOpenIntent intentGenerator,
707+
IntentGeneratorForAttachingToNotifications intentGenerator,
710708
int grouplessNotifCount
711709
) {
712710
JSONObject fcmJson = notificationJob.getJsonPayload();
@@ -763,7 +761,7 @@ private static void createGrouplessSummaryNotification(
763761

764762
private static Intent createBaseSummaryIntent(
765763
int summaryNotificationId,
766-
GenerateNotificationOpenIntent intentGenerator,
764+
IntentGeneratorForAttachingToNotifications intentGenerator,
767765
JSONObject fcmJson,
768766
String group
769767
) {
@@ -1034,7 +1032,7 @@ static BigInteger getAccentColor(JSONObject fcmJson) {
10341032

10351033
private static void addNotificationActionButtons(
10361034
JSONObject fcmJson,
1037-
GenerateNotificationOpenIntent intentGenerator,
1035+
IntentGeneratorForAttachingToNotifications intentGenerator,
10381036
NotificationCompat.Builder mBuilder,
10391037
int notificationId,
10401038
String groupSummary

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

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,31 @@
11
package com.onesignal
22

3-
import android.app.PendingIntent
43
import android.content.Context
54
import android.content.Intent
6-
import androidx.annotation.RequiresApi
75

86
class GenerateNotificationOpenIntent(
97
private val context: Context,
108
private val intent: Intent?,
119
private val startApp: Boolean
1210
) {
13-
14-
private val notificationOpenedClassAndroid23Plus: Class<*> = NotificationOpenedReceiver::class.java
15-
private val notificationOpenedClassAndroid22AndOlder: Class<*> = NotificationOpenedReceiverAndroid22AndOlder::class.java
16-
17-
fun getNewBaseIntent(
18-
notificationId: Int,
19-
): Intent {
20-
val intent =
21-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
22-
getNewBaseIntentAndroidAPI23Plus()
23-
else
24-
getNewBaseIntentAndroidAPI22AndOlder()
25-
26-
return intent
27-
.putExtra(
28-
GenerateNotification.BUNDLE_KEY_ANDROID_NOTIFICATION_ID,
29-
notificationId
30-
)
31-
// We use SINGLE_TOP and CLEAR_TOP as we don't want more than one OneSignal invisible click
32-
// tracking Activity instance around.
33-
.addFlags(
34-
Intent.FLAG_ACTIVITY_SINGLE_TOP or
35-
Intent.FLAG_ACTIVITY_CLEAR_TOP
36-
)
37-
}
38-
39-
@RequiresApi(android.os.Build.VERSION_CODES.M)
40-
private fun getNewBaseIntentAndroidAPI23Plus(): Intent {
41-
return Intent(
42-
context,
43-
notificationOpenedClassAndroid23Plus
44-
)
45-
}
46-
47-
// See NotificationOpenedReceiverAndroid22AndOlder.kt for details
48-
@Deprecated("Use getNewBaseIntentAndroidAPI23Plus instead for Android 6+")
49-
private fun getNewBaseIntentAndroidAPI22AndOlder(): Intent {
50-
val intent = Intent(
51-
context,
52-
notificationOpenedClassAndroid22AndOlder
53-
)
54-
intent.addFlags(
55-
Intent.FLAG_ACTIVITY_NEW_TASK or
56-
Intent.FLAG_ACTIVITY_MULTIPLE_TASK or
57-
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
58-
)
59-
return intent
11+
fun getIntentVisible(): Intent? {
12+
if (intent != null) return intent
13+
return getIntentAppOpen()
6014
}
6115

6216
/**
63-
* Creates a PendingIntent to attach to the notification click and it's action button(s).
64-
* If the user interacts with the notification this normally starts the app or resumes it
65-
* unless the app developer disables this via a OneSignal meta-data AndroidManifest.xml setting
66-
*
67-
* The default behavior is to open the app in the same way an Android homescreen launcher does.
17+
* This opens the app in the same way an Android homescreen launcher does.
6818
* This means we expect the following behavior:
6919
* 1. Starts the Activity defined in the app's AndroidManifest.xml as "android.intent.action.MAIN"
7020
* 2. If the app is already running, instead the last activity will be resumed
7121
* 3. If the app is not running (due to being push out of memory), the last activity will be resumed
7222
* 4. If the app is no longer in the recent apps list, it is not resumed, same as #1 above.
73-
* - App is removed from the recent app's list if it is swiped away or "clear all" is pressed.
23+
* - App is removed from the recent app's list if it is swiped away or "clear all" is pressed.
7424
*/
75-
fun getNewActionPendingIntent(
76-
requestCode: Int,
77-
oneSignalIntent: Intent,
78-
): PendingIntent? {
79-
val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
80-
return PendingIntent.getActivity(
81-
context,
82-
requestCode,
83-
oneSignalIntent,
84-
flags
85-
)
86-
}
87-
88-
// Return the provide intent if one was set, otherwise default to opening the app.
89-
fun getIntentVisible(): Intent? {
90-
if (intent != null) return intent
91-
return getIntentAppOpen()
92-
}
93-
94-
// Provides the default launcher Activity, if the app has one.
95-
// - This is almost always true, one of the few exceptions being an app that is only a widget.
9625
private fun getIntentAppOpen(): Intent? {
9726
if (!startApp) return null
9827

28+
// Is null for apps that only provide a widget for it's UI.
9929
val launchIntent =
10030
context.packageManager.getLaunchIntentForPackage(
10131
context.packageName
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.onesignal
2+
3+
import android.app.PendingIntent
4+
import android.content.Context
5+
import android.content.Intent
6+
import androidx.annotation.RequiresApi
7+
8+
class IntentGeneratorForAttachingToNotifications(
9+
val context: Context
10+
) {
11+
private val notificationOpenedClassAndroid23Plus: Class<*> = NotificationOpenedReceiver::class.java
12+
private val notificationOpenedClassAndroid22AndOlder: Class<*> = NotificationOpenedReceiverAndroid22AndOlder::class.java
13+
14+
fun getNewBaseIntent(
15+
notificationId: Int,
16+
): Intent {
17+
val intent =
18+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
19+
getNewBaseIntentAndroidAPI23Plus()
20+
else
21+
getNewBaseIntentAndroidAPI22AndOlder()
22+
23+
return intent
24+
.putExtra(
25+
GenerateNotification.BUNDLE_KEY_ANDROID_NOTIFICATION_ID,
26+
notificationId
27+
)
28+
// We use SINGLE_TOP and CLEAR_TOP as we don't want more than one OneSignal invisible click
29+
// tracking Activity instance around.
30+
.addFlags(
31+
Intent.FLAG_ACTIVITY_SINGLE_TOP or
32+
Intent.FLAG_ACTIVITY_CLEAR_TOP
33+
)
34+
}
35+
36+
@RequiresApi(android.os.Build.VERSION_CODES.M)
37+
private fun getNewBaseIntentAndroidAPI23Plus(): Intent {
38+
return Intent(
39+
context,
40+
notificationOpenedClassAndroid23Plus
41+
)
42+
}
43+
44+
// See NotificationOpenedReceiverAndroid22AndOlder.kt for details
45+
@Deprecated("Use getNewBaseIntentAndroidAPI23Plus instead for Android 6+")
46+
private fun getNewBaseIntentAndroidAPI22AndOlder(): Intent {
47+
val intent = Intent(
48+
context,
49+
notificationOpenedClassAndroid22AndOlder
50+
)
51+
intent.addFlags(
52+
Intent.FLAG_ACTIVITY_NEW_TASK or
53+
Intent.FLAG_ACTIVITY_MULTIPLE_TASK or
54+
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
55+
)
56+
return intent
57+
}
58+
59+
fun getNewActionPendingIntent(
60+
requestCode: Int,
61+
oneSignalIntent: Intent,
62+
): PendingIntent? {
63+
val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
64+
return PendingIntent.getActivity(
65+
context,
66+
requestCode,
67+
oneSignalIntent,
68+
flags
69+
)
70+
}
71+
}

0 commit comments

Comments
 (0)