|
1 | 1 | package com.wix.reactnativenotifications.core;
|
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.app.PendingIntent;
|
| 5 | +import android.app.TaskStackBuilder; |
4 | 6 | import android.content.Context;
|
5 | 7 | import android.content.Intent;
|
6 |
| -import android.os.Build; |
7 | 8 | import android.os.Bundle;
|
8 | 9 |
|
9 | 10 | import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
|
10 | 11 |
|
11 | 12 | public class NotificationIntentAdapter {
|
12 | 13 | private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification";
|
13 | 14 |
|
| 15 | + @SuppressLint("UnspecifiedImmutableFlag") |
14 | 16 | public static PendingIntent createPendingNotificationIntent(Context appContext, PushNotificationProps notification) {
|
15 |
| - Intent intent = new AppLaunchHelper().getLaunchIntent(appContext); |
16 |
| - intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle()); |
17 |
| - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
18 |
| - return PendingIntent.getActivity(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); |
| 17 | + if (canHandleTrampolineActivity(appContext)) { |
| 18 | + Intent intent = new Intent(appContext, ProxyService.class); |
| 19 | + intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle()); |
| 20 | + return PendingIntent.getService(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT); |
19 | 21 | } else {
|
20 |
| - return PendingIntent.getActivity(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT); |
| 22 | + Intent mainActivityIntent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName()); |
| 23 | + mainActivityIntent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle()); |
| 24 | + TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(appContext); |
| 25 | + taskStackBuilder.addNextIntentWithParentStack(mainActivityIntent); |
| 26 | + return taskStackBuilder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE); |
21 | 27 | }
|
22 | 28 | }
|
23 | 29 |
|
| 30 | + public static boolean canHandleTrampolineActivity(Context appContext) { |
| 31 | + return android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.R || appContext.getApplicationInfo().targetSdkVersion < 31; |
| 32 | + } |
| 33 | + |
| 34 | + public static Bundle extractPendingNotificationDataFromIntent(Intent intent) { |
| 35 | + return intent.getBundleExtra(PUSH_NOTIFICATION_EXTRA_NAME); |
| 36 | + } |
| 37 | + |
24 | 38 | public static boolean canHandleIntent(Intent intent) {
|
25 | 39 | if (intent != null) {
|
26 | 40 | Bundle notificationData = intent.getExtras();
|
|
0 commit comments