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

[Android] Opening app from the notification when app is dead not working correctly #326

Closed
llostris opened this issue Apr 29, 2019 · 8 comments
Assignees

Comments

@llostris
Copy link

We've just upgraded to the latest version of the library, and we've run into an issue when opening the app from notification - if the app is "dead" (not running in the background), and the user clicks on the notification, the PendingNotifications.getInitialNotification() is not triggered at all. We used that to navigate to a specific place in the app using some data passed in the notification, but now it's not working.

After some digging I've found this is related to the FCM implementation, and (most likely) the fact that we send both data and notification in the body of the notification. When opening the app from the notification in this case (when app is "dead"), data is meant to be passed as extras to the opening Intent, which seems to not be picked up as initial notification here.

How can we handle this situation?

react-native-notification version we're using: 1.3.0

@llostris llostris changed the title [Android] Opening app from the notification when app is dead [Android] Opening app from the notification when app is dead not working correctly May 13, 2019
@llostris
Copy link
Author

llostris commented Jul 1, 2019

If anyone has the same issue, a fix might be adding this to your MainActivity onCreate method:

        Bundle notificationData = getIntent().getExtras();
        if (notificationData != null) {
            final IPushNotification notification = PushNotification.get(getApplicationContext(), notificationData);
            if (notification != null) {
                notification.onOpened();
            }
        }

After adding this, if a notification was received while app was killed / in a dead state, when we launch the app getInitialNotification is working as expected, and we can handle the notification accordingly.

@yogevbd yogevbd self-assigned this Jul 28, 2019
@stale
Copy link

stale bot commented Aug 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the 🏚 stale label Aug 27, 2019
@stale
Copy link

stale bot commented Sep 3, 2019

The issue has been closed for inactivity.

@jsina
Copy link

jsina commented Oct 24, 2019

@llostris I have same problem with react-native-navigation v3 and I've got this error with your workaround:
I think because we extends NavigationActivity on mainActivity may cause these kind of issue,
share with me if you have any workaround...

/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:10: error: illegal start of type
  if (notificationData != null) {
  ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:10: error: <identifier> expected
  if (notificationData != null) {
                      ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:10: error: ';' expected
  if (notificationData != null) {
                         ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:10: error: illegal start of type
  if (notificationData != null) {
                              ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:10: error: <identifier> expected
  if (notificationData != null) {
                               ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:10: error: ';' expected
  if (notificationData != null) {
                                 ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:12: error: illegal start of type
      if (notification != null) {
      ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:12: error: <identifier> expected
      if (notification != null) {
                      ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:12: error: ';' expected
      if (notification != null) {
                         ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:12: error: illegal start of type
      if (notification != null) {
                              ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:12: error: <identifier> expected
      if (notification != null) {
                               ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:12: error: ';' expected
      if (notification != null) {
                                 ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:13: error: illegal start of type
          notification.onOpened();
                      ^
/Users/jsinamaleki/Documents/project/hitalent/clientapp/android/app/src/main/java/com/hitalent/hitalent/MainActivity.java:15: error: class, interface, or enum expected
  }
  ^
14 errors

@llostris
Copy link
Author

llostris commented Oct 24, 2019

@jsina illegal start of type indicates a syntax error somewhere in your code, possibly before the line the error first appears (the line above?). Maybe a missing semicolon?

@jsina
Copy link

jsina commented Oct 24, 2019

@llostris the app is compiled but the PendingNotifications.getInitialNotification() data is always undefined
here my MainApplication.java
Note because I'm using "react-native-notifications": "^2.0.6", I'm not creating NotificationsLifecycleFacade and use defaultFacade.

package com.hitalent.hitalent;

import android.app.Application;
import android.util.Log;
import android.content.Context;
import androidx.multidex.MultiDex;

import com.facebook.react.PackageList;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.ReactApplication;
import com.wix.reactnativenotifications.RNNotificationsPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;

import com.reactnativenavigation.NavigationApplication;
import com.reactnativenavigation.react.NavigationReactNativeHost;
import com.reactnativenavigation.react.ReactGateway;
import com.wix.reactnativenotifications.core.notification.INotificationsApplication;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.wix.reactnativenotifications.core.notification.IPushNotification;

import android.os.Bundle;


import java.util.List;

public class MainApplication extends NavigationApplication implements INotificationsApplication{

  @Override
  protected void attachBaseContext(Context base) {
      super.attachBaseContext(base);
      MultiDex.install(this);
  }

  @Override
  protected ReactGateway createReactGateway() {
    ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
      @Override
      protected String getJSMainModuleName() {
        return "index";
      }
    };
    return new ReactGateway(this, isDebug(), host);
  }

  @Override
  public boolean isDebug() {
    return BuildConfig.DEBUG;
  }

  protected List<ReactPackage> getPackages() {
    @SuppressWarnings("UnnecessaryLocalVariable")
    List<ReactPackage> packages = new PackageList(this).getPackages();
    // Packages that cannot be autolinked yet can be added manually here, for example:
    // packages.add(new MyReactNativePackage());
    packages.add(new RNNotificationsPackage(MainApplication.this));
    return packages;
  }

  @Override
  public List<ReactPackage> createAdditionalReactPackages() {
    return getPackages();
  }

  private AppLifecycleFacade notificationsLifecycleFacade;

  @Override
  public void onCreate() {
      super.onCreate();
      //...

      // Create an object of the custom facade impl
      notificationsLifecycleFacade = new NotificationsLifecycleFacade();
      // Attach it to react-native-navigation
      setActivityCallbacks(notificationsLifecycleFacade);
  }

  @Override
  public IPushNotification getPushNotification(Context context, Bundle bundle, AppLifecycleFacade defaultFacade, AppLaunchHelper defaultAppLaunchHelper) {
    return new CustomPushNotification(
      context,
      bundle,
      defaultFacade,
      defaultAppLaunchHelper,
      new JsIOHelper()
    );
  }
}

and here is CustomPushNotification.java file

package com.hitalent.hitalent;

import android.content.Context;
import android.os.Bundle;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.wix.reactnativenotifications.core.notification.PushNotification;

public class CustomPushNotification extends PushNotification {
    public CustomPushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) {
        super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper);
    }
}

@ghasemikasra39
Copy link

Dear @llostris
My onCreate method is:

    protected void onCreate(Bundle savedInstanceState) {
         // Use SplashTheme in AndroidManifest.xml for MainActivity, themes loads before layouts inflate
         setTheme(R.style.AppTheme); // Now set the theme from Splash to App before setContentView
         setContentView(R.layout.launch_screen); // Then inflate the new view
         SplashScreen.show(this); // Now show the splash screen. Hide it later in JS
         super.onCreate(savedInstanceState);
    }

Where should I add this ?

    Bundle notificationData = getIntent().getExtras();
        if (notificationData != null) {
            final IPushNotification notification = PushNotification.get(getApplicationContext(), notificationData);
            if (notification != null) {
                notification.onOpened();
            }
        }
    "react-native": "0.62.2",
    "@react-native-firebase/app": "^7.1.0",
    "@react-native-firebase/messaging": "^7.1.1",

@llostris
Copy link
Author

@ghasemikasra39 In our app, we've simply added it add the end, so after super.onCreate(savedInstanceState):

protected void onCreate(Bundle savedInstanceState) {
    // Use SplashTheme in AndroidManifest.xml for MainActivity, themes loads before layouts inflate
    setTheme(R.style.AppTheme); // Now set the theme from Splash to App before setContentView
    setContentView(R.layout.launch_screen); // Then inflate the new view
    SplashScreen.show(this); // Now show the splash screen. Hide it later in JS
    super.onCreate(savedInstanceState);

     // Handle initial notification if the app was in dead state
    Bundle notificationData = getIntent().getExtras();
    if (notificationData != null) {
        final IPushNotification notification = PushNotification.get(getApplicationContext(), notificationData);
        if (notification != null) {
            notification.onOpened();
        }
    }
}

However, note that this was resolving the problem for react-native-notification ver. 1.3.0, which is a pretty old version – I don't know if it works with the latest react-native-notifications version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants