Open viktor-CCM opened 2 years ago
I faced the same issue
I have this problem too
I created a new project from template RN(npx react-native init AwesomeProject). I used the guide from the documentation for the integration Android part.
I sent the following HTTP POST request to https://fcm.googleapis.com/fcm/send (via Postman). I got the push notification. But if app killed Notifications.getInitialNotification() return undefined.
I have the same problem. Could someone help with it?
@DanielEliraz I saw your first fix in PR. I checked it locally. It doesn't work.
I am seeing this same issue but also noticed that the initial props on my
EDIT: Here is my working proof of concept
#push-notification-helper.ts
... {include all the stuff from the docs}
export const loadInitialNotification = (initialNotification?: Notification) => {
Notifications.getInitialNotification().then(async (notification = initialNotification) => {
console.log(`getInitialNotification opened:`, notification);
if (notification)
onNotification(notification, AnalyticsLabel.Cold);
else {
store.dispatch(generalActionSetInitialNavigationState(undefined));
}
Notifications.events().registerNotificationOpened((notification2, completion) => {
console.log(`Notification opened: `, notification2);
onNotification(notification2, AnalyticsLabel.Warm);
completion();
});
});
};
#App.tsx
export default class App extends Component {
constructor(props: AppProps) {
super(props);
const notification = Platform.OS === 'android' && !!props.android_notification_info && ({
identifier: props.android_notification_info.google?.message_id,
payload: {data: props.android_notification_info},
title: props.android_notification_info.title,
body: props.android_notification_info.body,
sound: '',
badge: 1,
type: '',
thread: '',
}) as Notification || undefined;
loadInitialNotification(notification);
}
...
}
@viktor-CCM do you expect that getInitialNotification
will return the notification only when the application is in a kill state, while registerNotificationOpened
will be triggered only when the application is in a background state?
@viktor-CCM do you expect that
getInitialNotification
will return the notification only when the application is in a kill state, whileregisterNotificationOpened
will be triggered only when the application is in a background state?
@DanielEliraz Yes.
I think I found a way to trigger the registerNotificationOpened
also in kill state. do you think it's an advantage or you prefer the way it is now?
@viktor-CCM do you expect that
getInitialNotification
will return the notification only when the application is in a kill state, whileregisterNotificationOpened
will be triggered only when the application is in a background state?@DanielEliraz Yes.
I think I found a way to trigger the
registerNotificationOpened
also in kill state. do you think it's an advantage or you prefer the way it is now?@viktor-CCM do you expect that
getInitialNotification
will return the notification only when the application is in a kill state, whileregisterNotificationOpened
will be triggered only when the application is in a background state?@DanielEliraz Yes.
I like your way with registerNotificationOpened. But it will be breaking changes and it will be difficult migration (e.x different documentation for versions, migration guide). All other libs use getInitialNotification, it is a common way.
It seems to fix specifically the getInitialNotification issue, but brings back https://github.com/wix/react-native-notifications/pull/837 Ideally everything should work like it did before Android SDK 31, without changing any behavior. I'm not an Android dev, but is there something I can help you with? If you give me any insights or unfinished patches, I can try on my end.
@DanielEliraz this seems to fix the issue for us https://github.com/RocketChat/Rocket.Chat.ReactNative/pull/4648/commits/929ec66cc2b1a1e0045d674379ad0a1d5fb6699d
Is there any way to call mAppLifecycleFacade
directly instead of doing that stuff?
@DanielEliraz @diegolmello any news on this issue ? we are facing the same problem. Is there a workaround ? Do you need support somehow ?
@StefanLuecke Our fix is partial only. We found it to fix it only for the first push notification tap. After that, any new push notification tap is going to redirect to the first push again.
We're going to test 4.3.3, because they released official support to RN 0.68 and targeting SDK 31, so maybe there's a fix https://github.com/wix/react-native-notifications/pull/907 However it's very sad we don't get any updates about any of those issues we've been suffering with.
@diegolmello @DanielEliraz
We debugged the Android stuff and figured out that the function callOnOpenedIfNeed
in RNNotificationsPackage is causing the Error.
we changed
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();
}
}
}
to
private void callOnOpenedIfNeed(Activity activity) {
Intent intent = activity.getIntent();
if (NotificationIntentAdapter.canHandleIntent(intent)) {
Context appContext = mApplication.getApplicationContext();
Bundle notificationData = intent.getExtras();
final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
if (pushNotification != null) {
pushNotification.onOpened();
}
}
}
because the function NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent)
is returning null.
We forked this repo. maybe you can check if this works for you as well.
we figured out some more issues i.e. the function onNewIntent
in class RNNotificationsModule is never called.
@StefanLuecke your patch seems to have fixed the bug for us. Thanks!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
The issue has been closed for inactivity.
@StefanLuecke thanks for the patch, I really appreciate that you spent your time to investigate the issue and share the solution 🤗
I'm playing with the solution you proposed and the getInitialNotification
callback does fire now when the app launched from the killed state.
However, I noticed that whenever I resume the app (e.g. by minimising it and opening it back from task manager), the registerNotificationOpened
callback is called every time with the initial notification payload. Here is a video that demonstrates it.
It seems that resuming the app fires the onActivityStarted callback. Then, the callOnOpenedIfNeed function fires and since the activity initially was launched with a notification, then the activity.getIntent()
will contain the push notification bundle and it will propagate to JS.
I managed to fix it by clearing the intent once the push notification is handled, but I wonder if it won't have any undesired effect 🤔
activity.setIntent(null)
caused our app to crash on any android version less than 13. activity.setIntent(new Intent())
seems to work better as we continue to test it.
@bhandanyan-nomad version 5.0.0 was released with activity.setIntent(new Intent())
, can you please verify it?
@DanielEliraz we ended up not using a patch because even with activity.setIntent(new Intent())
we found our app to still be crashing on older androids.
We ended up fixing all our android related linking issues when we upgraded to version 4.3.5
of this package. We will try 5.0.0
on the old device to see if it is still working properly or if the crashing comes back.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
@DanielEliraz we ended up not using a patch because even with
activity.setIntent(new Intent())
we found our app to still be crashing on older androids.We ended up fixing all our android related linking issues when we upgraded to version
4.3.5
of this package. We will try5.0.0
on the old device to see if it is still working properly or if the crashing comes back.
How were your results on older devices?
Notifications.getInitialNotification() return undefined when the application is in a kill state. If the application is foreground and the user tap on the notification - Notifications.events().registerNotificationOpened works properly.
Case 1 (Android) Dead state (100% dead) Click on the notification message App will open Notifications.getInitialNotification() return undefined
In iOS it works properly.
"react-native-notifications": "^4.3.1", "react-native": "^0.69.6",
// Android Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...........">
Google service package
// Test devices OnePlus 9R android 12 Pixel 4 API 30 (simulator)