Skip to content

Revert "Fix for Android trampoline restriction" #969

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

Merged
merged 5 commits into from
May 22, 2023
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
6 changes: 6 additions & 0 deletions lib/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
package="com.wix.reactnativenotifications">

<application>

<!--
A proxy-service that gives the library an opportunity to do some work before launching/resuming the actual application task.
-->
<service android:name=".core.ProxyService"/>

<service
android:name=".fcm.FcmInstanceIdListenerService"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
import com.wix.reactnativenotifications.core.notificationdrawer.IPushNotificationsDrawer;
import com.wix.reactnativenotifications.core.notificationdrawer.PushNotificationsDrawer;

Expand Down Expand Up @@ -62,18 +61,14 @@ public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(mApplication.getApplicationContext());
notificationsDrawer.onNewActivity(activity);

Intent intent = activity.getIntent();
if (NotificationIntentAdapter.canHandleIntent(intent)) {
Bundle notificationData = intent.getExtras();
final IPushNotification pushNotification = PushNotification.get(mApplication.getApplicationContext(), notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
}
callOnOpenedIfNeed(activity);
}

@Override
public void onActivityStarted(Activity activity) {
if (InitialNotificationHolder.getInstance().get() == null) {
callOnOpenedIfNeed(activity);
}
}

@Override
Expand All @@ -95,4 +90,17 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
@Override
public void onActivityDestroyed(Activity activity) {
}

private void callOnOpenedIfNeed(Activity activity) {
Intent intent = activity.getIntent();
if (NotificationIntentAdapter.canHandleIntent(intent)) {
Context appContext = mApplication.getApplicationContext();
Bundle notificationData = NotificationIntentAdapter.canHandleTrampolineActivity(appContext) ?
intent.getExtras() : NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
package com.wix.reactnativenotifications.core;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import com.wix.reactnativenotifications.core.notification.PushNotificationProps;

public class NotificationIntentAdapter {
private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification";

@SuppressLint("UnspecifiedImmutableFlag")
public static PendingIntent createPendingNotificationIntent(Context appContext, PushNotificationProps notification) {
Intent intent = new AppLaunchHelper().getLaunchIntent(appContext);
intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return PendingIntent.getActivity(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
if (canHandleTrampolineActivity(appContext)) {
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);
} else {
return PendingIntent.getActivity(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
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_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
}
}

public static boolean canHandleTrampolineActivity(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);
}

public static boolean canHandleIntent(Intent intent) {
if (intent != null) {
Bundle notificationData = intent.getExtras();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.wix.reactnativenotifications.core;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.wix.reactnativenotifications.BuildConfig;
import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;

public class ProxyService extends IntentService {

private static final String TAG = ProxyService.class.getSimpleName();

public ProxyService() {
super("notificationsProxyService");
}

@Override
protected void onHandleIntent(Intent intent) {
if (BuildConfig.DEBUG) Log.d(TAG, "New intent: "+intent);
final Bundle notificationData = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
final IPushNotification pushNotification = PushNotification.get(this, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,22 @@ protected int postNotification(Integer notificationId) {
protected void digestNotification() {
if (!mAppLifecycleFacade.isReactInitialized()) {
setAsInitialNotification();
launchOrResumeApp();
return;
}

final ReactContext reactContext = mAppLifecycleFacade.getRunningReactContext();
if (reactContext.getCurrentActivity() == null) {
setAsInitialNotification();
return;
}

dispatchImmediately();
if (mAppLifecycleFacade.isAppVisible()) {
dispatchImmediately();
} else if (mAppLifecycleFacade.isAppDestroyed()) {
launchOrResumeApp();
} else {
dispatchUponVisibility();
}
}

protected PushNotificationProps createProps(Bundle bundle) {
Expand All @@ -121,6 +127,17 @@ protected void dispatchImmediately() {
notifyOpenedToJS();
}

protected void dispatchUponVisibility() {
mAppLifecycleFacade.addVisibilityListener(getIntermediateAppVisibilityListener());

// Make the app visible so that we'll dispatch the notification opening when visibility changes to 'true' (see
// above listener registration).
launchOrResumeApp();
}

protected AppVisibilityListener getIntermediateAppVisibilityListener() {
return mAppVisibilityListener;
}

protected Notification buildNotification(PendingIntent intent) {
return getNotificationBuilder(intent).build();
Expand Down Expand Up @@ -195,6 +212,13 @@ private void notifyOpenedToJS() {
mJsIOHelper.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, response, mAppLifecycleFacade.getRunningReactContext());
}

protected void launchOrResumeApp() {
if (NotificationIntentAdapter.canHandleTrampolineActivity(mContext)) {
final Intent intent = mAppLaunchHelper.getLaunchIntent(mContext);
mContext.startActivity(intent);
}
}

private int getAppResourceId(String resName, String resType) {
return mContext.getResources().getIdentifier(resName, resType, mContext.getPackageName());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@
"html"
]
}
}
}
24 changes: 12 additions & 12 deletions scripts/test-unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,38 @@ function run() {

function runAndroidUnitTests() {
const conf = release ? 'testReactNative60ReleaseUnitTest' : 'testReactNative60DebugUnitTest';
if (android && process.env.JENKINS_CI) {
const sdkmanager = '/usr/local/share/android-sdk/tools/bin/sdkmanager';
exec.execSync(`yes | ${sdkmanager} --licenses`);
// exec.execSync(`echo y | ${sdkmanager} --update && echo y | ${sdkmanager} --licenses`);
}
exec.execSync(`cd lib/android && ./gradlew ${conf}`);
}

function runIosUnitTests() {
const conf = release ? `Release` : `Debug`;
exec.execSync('npm run build');
exec.execSync('npm run pod-install');
testTarget('NotificationsExampleApp', 'iPhone 12', '14.4');
}

function testTarget(scheme, device, OS = 'latest') {
const conf = release ? `Release` : `Debug`;
exec.execSync(`cd ./example/ios &&
RCT_NO_LAUNCH_PACKAGER=true
xcodebuild build build-for-testing
-scheme "NotificationsExampleApp"
-scheme "${scheme}"
-workspace NotificationsExampleApp.xcworkspace
-sdk iphonesimulator
-configuration ${conf}
-derivedDataPath ./example/ios/DerivedData/NotificationsExampleApp
-derivedDataPath ./DerivedData/NotificationsExampleApp
-quiet
-UseModernBuildSystem=NO
-UseModernBuildSystem=YES
ONLY_ACTIVE_ARCH=YES`);

exec.execSync(`cd ./example/ios &&
RCT_NO_LAUNCH_PACKAGER=true
xcodebuild test-without-building
-scheme "NotificationsExampleApp"
-scheme "${scheme}"
-workspace NotificationsExampleApp.xcworkspace
-sdk iphonesimulator
-configuration ${conf}
-destination 'platform=iOS Simulator,name=iPhone 11'
-derivedDataPath ./example/ios/DerivedData/NotificationsExampleApp
-destination 'platform=iOS Simulator,name=${device},OS=${OS}'
-derivedDataPath ./DerivedData/NotificationsExampleApp
ONLY_ACTIVE_ARCH=YES`);
}

Expand Down