Comments (12)
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.
@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.
@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.
@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.
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.
@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.
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 ReactApplication
in 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.
@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.
@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.
Sorry to hear we couldn't work it out.
from react-native-notifications.
I am facing the same problem too. Everything gives the correct response.
- App initialized => publishing existing token (eL9jaDEM...
- 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.
for me adding android.enableAapt2=false
in gradle.properties fixed the problem
from react-native-notifications.
Related Issues (20)
- registerNotificationOpened is not called when app is in foreground. HOT 1
- Crash app when recieve Notification in IOS 14 HOT 6
- Fix API Documentation so that postLocalNotification works - Includes fix HOT 1
- [iOS] getInitialNotification returnsthe notification even when the app was opened without tapping it HOT 2
- app crashes receiving a notification during startup
- How do I even use this package?
- registerNotificationOpened isn't working as expected in iOS HOT 1
- `startObserving` method call is happening post send event call
- Android 10 and below: NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getFlags()' on a null object reference HOT 1
- Invariant Violation: new NativeEventEmitter() requires a non-null argument.
- Update Robolectric version
- Subscriptions to topics
- "$" getting copied HOT 1
- Code that setting NotificationChannel sound on Android might lead unexpected consequences HOT 1
- Application does not trigger registerNotificationOpened when it is closed, only when it is in the background.
- I can't get notifications to open my app HOT 1
- How to undo `Notifications.registerRemoteNotifications()` ? HOT 3
- Minification failure with R8: Compilation failed for ReactAppLifecycleFacade$1 in react-native-notifications
- Not receiving any notification on iOS release mode only HOT 1
- Huawei push notification in library
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-notifications.