Giter Site home page Giter Site logo

Comments (12)

pennersr avatar pennersr commented on July 24, 2024

I am not sure, but possibly this is caused by the fact that the token received event triggers in Java while the JS engine is not running yet, causing the event to get lost. You might want to give an explicit NotificationsAndroid.refreshToken() token a try as it should re-trigger sending the token to JS.

from react-native-notifications.

sachin-sable avatar sachin-sable commented on July 24, 2024

@pennersr : Thank you for the reply. I was on vacation. I tried your suggestion today but it is not affecting anything. I have debugged the source code of React native pushnotification and the point where it sends the token to JS has React context null.
Filename: GcmToken.java
Method : sendTokenToJS()
Line : final ReactContext reactContext = instanceManager.getCurrentReactContext();

Here the reactContext is always coming null. :(

from react-native-notifications.

d4vidi avatar d4vidi commented on July 24, 2024

@sachin-sable please try to check whether this log appears when the app is loaded / device is run:
GCM is refreshing token... instanceId=<instance-id>

Also double check that you've defined the sender ID in your manifest.

from react-native-notifications.

sachin-sable avatar sachin-sable commented on July 24, 2024

@d4vidi Yes. I have checked both the points. And I am getting the log which you mentioned with instanceId and a Token.

from react-native-notifications.

d4vidi avatar d4vidi commented on July 24, 2024

I see. In that case, have you tried calling NotificationsAndroid.refreshToken() explicitly when your react native activity (app) is ready? Please try to do so, and try to look up a log that says: Manual token refresh => ...

Also, could you copy&paste the log that says App initialized => ... ? (token excluded)

from react-native-notifications.

sachin-sable avatar sachin-sable commented on July 24, 2024

@d4vidi : Thank you for your attention on this.
I have tried following:

Implemented the ReactInstanceManager.ReactInstanceEventListener and overridden the following method,

@Override
public void onReactContextInitialized(ReactContext context) {
       
        if(context!=null) {
            context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit("refreshtoken", null);
        }
}

I had the even listener in my js code for above event. There I called the refreshToken method:

    DeviceEventEmitter.addListener("refreshtoken", () => {
         NotificationsAndroid.refreshToken();

    });

Following are the logs:

-ReactNativeNotifs:Running application "ReactApp" with appParams: {"initialProps":{"APP_NAME”:”ReactApp”,”USER_ID”:1},”rootTag":1}. __DEV__ === false, development-level warning are OFF, performance optimizations are ON

-ReactNativeNotifs:App initialized => asking for new token
-ReactNativeNotifs:GCM is refreshing token... instanceId=cRmglHPl_u0
-GCM has a new token: instanceId=cRmglHPl_u0, token=cRmglHPl_u0:AP…

..



-ReactNativeNotifs: Native method invocation: refreshToken()
-ReactNativeNotifs: Manual token refresh => publishing existing token (cRmglHPl_u0:AP…)

from react-native-notifications.

d4vidi avatar d4vidi commented on July 24, 2024

In that case, I would double check that you're properly setting up the main react activity and the app, either as described by react native's android bootstrap guide, or by inheriting ReactActivity and implementing ReactApplicationin your react activity and application, respectively.
I'm saying this because it appears as though your react instance manager isn't properly aware of the react contexts lifecycle...

Another question: are you using Wix' react-native-navigation?

from react-native-notifications.

sachin-sable avatar sachin-sable commented on July 24, 2024

@d4vidi Nopes. I am not using Wix' react-native-navigation.

Please see my code in activity and application classes.

private void initReactView(){
        mReactRootView = new ReactRootView(this.getApplicationContext());
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(getApplication())
                .setBundleAssetName("index.android.bundle")
                .setJSMainModuleName("index.android")
                .addPackage(new MainReactPackage())
                .addPackage(new RNNotificationsPackage(getApplication()))
                .addPackage(new ReactAppCallbackModules())
                .addPackage(new WebViewBridgePackage())
                .addPackage(new ReactNativeContacts())
                .addPackage(new AndroidKeyboardAdjustPackage(this))
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .build();
        mReactInstanceManager.addReactInstanceEventListener(this);
 }

private void startReactApplication() {
        Bundle launchOptions = new Bundle();
        launchOptions.putString("APP_NAME", getString(R.string.app_name).replace("\n", " "));
        launchOptions.putLong("USER_ID", AppUser.loadFromContext(this).ID);
        mReactRootView.startReactApplication(mReactInstanceManager, "ReactApp", launchOptions);
}

and my onCreate() has :

if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
        } else {
            startReactApplication();
}

I have also added the package inclusion in Application class:

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        protected boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new RNNotificationsPackage(CoreApplication.this)
            );
        }
    };

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }

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

If I don't add package inclusion in Activity, it gives me following exception:

com.facebook.react.common.JavascriptException: undefined is not an object (evaluating 'RNNotifications.refreshToken')

from react-native-notifications.

sachin-sable avatar sachin-sable commented on July 24, 2024

@d4vidi Thank you for your time. I have implemented my own GcmToken class by implementing IGMToken interface but without sending token to js. I send token to JS when my react context is initialised in ** onReactContextInitialized**.

Thanks.

from react-native-notifications.

d4vidi avatar d4vidi commented on July 24, 2024

Sorry to hear we couldn't work it out.

from react-native-notifications.

seanlau555 avatar seanlau555 commented on July 24, 2024

I am facing the same problem too. Everything gives the correct response.

  1. App initialized => publishing existing token (eL9jaDEM...
  2. Manual token refresh => publishing existing token (eL9jaDEMJxU:APA91bF ..
    However, it always responses null reactContext in sendTokenToJS.
    ( ReactNativeNotifs: send token reactContext: null )
    react-native-navigation : 1.1.193
    react-native-notifications : 1.1.14

from react-native-notifications.

wojteg1337 avatar wojteg1337 commented on July 24, 2024

for me adding android.enableAapt2=false in gradle.properties fixed the problem

from react-native-notifications.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.