Skip to content

Commit

Permalink
Fix trampoline on target sdk 31 (wix#837)
Browse files Browse the repository at this point in the history
* bump to rn 67 in example

* restrict trampoline fix to targetSdk 31 and above
  • Loading branch information
DanielEliraz authored and elliscwc committed Aug 15, 2022
1 parent 9a144ab commit 5107ee0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 25 deletions.
23 changes: 12 additions & 11 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext{
androidSdkVersion = 31
ext {
androidSdkVersion = 30
androidMinSdkVersion = 21
kotlinVersion = "1.3.61"
kotlinStdlib = "kotlin-stdlib-jdk8"
detoxKotlinVersion = kotlinVersion
}
}
repositories {
google()
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.2"
classpath "com.android.tools.build:gradle:4.2.2"
classpath "com.google.gms:google-services:4.3.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}

allprojects {
repositories {
mavenLocal()
mavenCentral()
google()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../../node_modules/react-native/android"
maven { url "$rootDir/../../node_modules/react-native/android" }
maven { url "$rootDir/../../node_modules/jsc-android/dist" }
mavenCentral {
content {
excludeGroup "com.facebook.react"
}
}
google()
maven { url 'https://www.jitpack.io' }
}
}

Expand Down
3 changes: 1 addition & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Thu Dec 12 22:59:18 IST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
12 changes: 6 additions & 6 deletions example/android/myapplication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ project.ext.react = [
enableHermes : true,
bundleInAlpha : true,
bundleInBeta : true,
hermesFlagsDebug:['-Xes6-proxy'],
hermesFlagsRelease:['-Xes6-proxy']
hermesFlagsDebug:['-Xes6-proxy','-output-source-map'],
hermesFlagsRelease:['-output-source-map'],
hermesCommand: "../../../node_modules/hermes-engine/%OS-BIN%/hermesc",
]

apply from: "../../../node_modules/react-native/react.gradle"
Expand Down Expand Up @@ -59,11 +60,10 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"

def hermesPath = "../../../node_modules/hermes-engine/android/";
exec {
executable 'grep'
args "version", '../../../node_modules/hermes-engine/package.json'
}
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
implementation "org.jetbrains.kotlin:$kotlinStdlib:$kotlinVersion"
Expand Down
1 change: 1 addition & 0 deletions example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
rootProject.name = 'NotificationsExampleApp'
include ':myapplication'

include ':react-native-notifications'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

Expand Down Expand Up @@ -94,9 +95,10 @@ public void onActivityDestroyed(Activity activity) {
private void callOnOpenedIfNeed(Activity activity) {
Intent intent = activity.getIntent();
if (NotificationIntentAdapter.canHandleIntent(intent)) {
Bundle notificationData = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R ?
Context appContext = mApplication.getApplicationContext();
Bundle notificationData = NotificationIntentAdapter.cannotHandleTrampolineActivity(appContext) ?
NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent) : intent.getExtras();
final IPushNotification pushNotification = PushNotification.get(mApplication.getApplicationContext(), notificationData);
final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ public class NotificationIntentAdapter {

@SuppressLint("UnspecifiedImmutableFlag")
public static PendingIntent createPendingNotificationIntent(Context appContext, PushNotificationProps notification) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
if (cannotHandleTrampolineActivity(appContext)) {
Intent mainActivityIntent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
mainActivityIntent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(appContext);
taskStackBuilder.addNextIntentWithParentStack(mainActivityIntent);
return taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
return taskStackBuilder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
} else {
Intent intent = new Intent(appContext, ProxyService.class);
intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
return PendingIntent.getService(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
}
}

public static boolean cannotHandleTrampolineActivity(Context appContext) {
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R && appContext.getApplicationInfo().targetSdkVersion >= 31;
}

public static Bundle extractPendingNotificationDataFromIntent(Intent intent) {
return intent.getBundleExtra(PUSH_NOTIFICATION_EXTRA_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void notifyOpenedToJS() {
}

protected void launchOrResumeApp() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.R) {
if (!NotificationIntentAdapter.cannotHandleTrampolineActivity(mContext)) {
final Intent intent = mAppLaunchHelper.getLaunchIntent(mContext);
mContext.startActivity(intent);
}
Expand Down
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,23 @@
"@babel/plugin-proposal-export-default-from": "7.2.0",
"@babel/plugin-proposal-export-namespace-from": "7.2.0",
"@types/jest": "24.9.0",
"@types/lodash": "4.14.170"
"@types/lodash": "4.14.170",
"@types/react": "16.9.35",
"@types/react-native": "0.67.1",
"@types/react-test-renderer": "16.9.2",
"babel-eslint": "10.0.3",
"detox": "^19.4.2",
"github-release-notes": "https://github.com/yogevbd/github-release-notes/tarball/e601b3dba72dcd6cba323c1286ea6dd0c0110b58",
"jest": "24.9.0",
"lodash": "4.17.21",
"metro-react-native-babel-preset": "0.66.2",
"react": "17.0.2",
"react-autobind": "1.0.6",
"react-native": "0.67.3",
"shell-utils": "1.0.10",
"ts-mockito": "2.5.0",
"tslint": "6.1.2",
"typescript": "^3.7.5"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down

0 comments on commit 5107ee0

Please # to comment.