Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trampoline on target sdk 31 #837

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
// 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()
mavenCentral()
}
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()
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.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
4 changes: 2 additions & 2 deletions example/android/myapplication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ project.ext.react = [
bundleInBeta : true,
hermesFlagsDebug:['-Xes6-proxy','-output-source-map'],
hermesFlagsRelease:['-output-source-map'],
hermesCommand: "../../../node_modules/react-native/node_modules/hermes-engine/%OS-BIN%/hermesc",
hermesCommand: "../../../node_modules/hermes-engine/%OS-BIN%/hermesc",
]

apply from: "../../../node_modules/react-native/react.gradle"
Expand Down Expand Up @@ -51,7 +51,7 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"

def hermesPath = "../../../node_modules/react-native/node_modules/hermes-engine/android/";
def hermesPath = "../../../node_modules/hermes-engine/android/";
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((int) System.currentTimeMillis(), 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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@types/jest": "24.9.0",
"@types/lodash": "4.14.170",
"@types/react": "16.9.35",
"@types/react-native": "0.66.12",
"@types/react-native": "0.67.1",
"@types/react-test-renderer": "16.9.2",
"babel-eslint": "10.0.3",
"detox": "^19.4.2",
Expand All @@ -64,7 +64,7 @@
"metro-react-native-babel-preset": "0.66.2",
"react": "17.0.2",
"react-autobind": "1.0.6",
"react-native": "0.66.3",
"react-native": "0.67.3",
"shell-utils": "1.0.10",
"ts-mockito": "2.5.0",
"tslint": "6.1.2",
Expand Down Expand Up @@ -131,4 +131,4 @@
"html"
]
}
}
}