|
1 | 1 | package com.onesignal
|
2 | 2 |
|
3 |
| -import android.app.PendingIntent |
4 | 3 | import android.content.Context
|
5 | 4 | import android.content.Intent
|
6 |
| -import androidx.annotation.RequiresApi |
7 | 5 |
|
8 | 6 | class GenerateNotificationOpenIntent(
|
9 | 7 | private val context: Context,
|
10 | 8 | private val intent: Intent?,
|
11 | 9 | private val startApp: Boolean
|
12 | 10 | ) {
|
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() |
60 | 14 | }
|
61 | 15 |
|
62 | 16 | /**
|
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. |
68 | 18 | * This means we expect the following behavior:
|
69 | 19 | * 1. Starts the Activity defined in the app's AndroidManifest.xml as "android.intent.action.MAIN"
|
70 | 20 | * 2. If the app is already running, instead the last activity will be resumed
|
71 | 21 | * 3. If the app is not running (due to being push out of memory), the last activity will be resumed
|
72 | 22 | * 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. |
74 | 24 | */
|
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. |
96 | 25 | private fun getIntentAppOpen(): Intent? {
|
97 | 26 | if (!startApp) return null
|
98 | 27 |
|
| 28 | + // Is null for apps that only provide a widget for it's UI. |
99 | 29 | val launchIntent =
|
100 | 30 | context.packageManager.getLaunchIntentForPackage(
|
101 | 31 | context.packageName
|
|
0 commit comments