Skip to content

Fix for Android trampoline restriction #920

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 1 commit into from
May 18, 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: 0 additions & 6 deletions lib/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
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,6 +17,7 @@
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 @@ -61,14 +62,18 @@ public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
final IPushNotificationsDrawer notificationsDrawer = PushNotificationsDrawer.get(mApplication.getApplicationContext());
notificationsDrawer.onNewActivity(activity);

callOnOpenedIfNeed(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();
}
}
}

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

@Override
Expand All @@ -90,17 +95,4 @@ 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,40 +1,26 @@
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) {
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);
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);
} else {
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);
return PendingIntent.getActivity(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
}
}

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,16 @@ 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;
}

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

protected PushNotificationProps createProps(Bundle bundle) {
Expand All @@ -127,17 +121,6 @@ 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 @@ -212,13 +195,6 @@ 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