Closed tyndria closed 9 months ago
Hi @tyndria
The onNotificationReceivedInForeground
is actually quite simple in its implementation logic. Whenever the AppDelegate's didReceiveRemoteNotification
is triggered, we take a look at the app state. If the app state is active, we forward the notification to the JS layer as a foreground notification event. In other words, it should not fire in all cases, only in foreground cases.
Looking at your video, it seems like there is a second notification being displayed after the app was initially launched. This is unexpected and I'm wondering if you can help me understand where that "inner notification" is coming from.
Hi again @tyndria
I tried building a new RN app using:
AppDelegate.h
you sharedAppDelegate.mm
you shared (with some minor tweaks as follows:)
RCTAppSetupPrepareApp(application, true);
UIView *rootView = RCTAppSetupDefaultRootView(bridge, @"ForegroundTest", initProps, true);
#import "RNSplashScreen.h"
[RNSplashScreen show];
Podfile
you sharedindex.js
you sharedApp.tsx
you shared (where I replaced the handlers with simple functions with console.log
statements)Unfortunately, I was unable to reproduce what you are seeing.
When I had my app active in the foreground:
onNotificationOpened
listener - but not my onNotificationReceivedInForeground
listener.Do you have any further details you can share with us? Would it be possible for you to share a project repo where this behavior can be seen so that we can clone it and further debug?
@cshfang hi, big thanks for such a quick response!
Please, give me several days to give you more info. For now I'm a bit confused about this problem, because I thought I fixed it by defining an empty onNotificationReceivedInBackground
(I know it sounds crazy):
Notifications.Push.onNotificationReceivedInBackground(noop);
But now this problem is just reproduced randomly, I can't catch a consistency here, so let me just recheck everything.
I only have a thought that maybe it's kind of a race or asynchronicity problem (because of the fact that this bug is floating for me now, sth onNotificationReceivedInForeground
invokes, sometimes doesn't).
Again, sorry for crazy ideas / solutions, I'm just fighting with push notifications for the second week
No problem! Please keep us posted.
@tyndria, were you able to find any way to reproduce the issue reliably or do any further testing on this?
@cwomack hi!
sorry for the late response, but this is what I’ve done:
Then I did the same with amplify push notifications and I stopped receiving additional inner notifications (but launch notifications weren’t working which could be just sth else, because I was in a hurry) So it seems that ‘react-native-splash-screen’ could be a problem, but I haven’t tested everything properly for amplify notifications
@tyndria, thanks for quick the reply. If you get a chance to properly test everything for Amplify Push Notifications then let us know if the issue persists. I'm curious to hear if replacing (or removing) the react-native-splash-screen
library also resolves any issues you saw on the Amplify side.
@tyndria what version of aws-amplify are you using as i am trying to use the latest 5.2.1 but its not working for me ?
@aalindQuantel hi! There is Environment information
in the issue description, check it, please
aws-amplify version was 5.1.4
what isn't working exactly?
Notifications.Push.enable(), Notifications.Push.onNotificationReceivedInBackground are not working
@aalindQuantel sorry, this issue is about redundant events, not about absence of events
I send a push notification when my app is active, onNotificationReceivedInForeground was raised => Ok. The notification also display to user, but when I click to push notification (my app still active), onNotificationOpened does not raise (instead onNotificationReceivedInForeground was raised again).
My device is Iphone 11, iOS 16.4.1. To allow iOS display notification when my app is active, here my code
AppDelegate.h
@interface AppDelegate : RCTAppDelegate <UNUserNotificationCenterDelegate>
AppDelegate.mm:
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
App.tsx:
function App(): JSX.Element {
const tokenReceivedHandlerEventListener = Notifications.Push.onTokenReceived(
myTokenReceivedHandler,
);
const notificationReceivedInForegroundListener =
Notifications.Push.onNotificationReceivedInForeground(
myNotificationReceivedHandler,
);
const notificationReceivedInBackgroundListener =
Notifications.Push.onNotificationReceivedInBackground(
myAsyncNotificationReceivedHandler,
);
const notificationOpenedListener = Notifications.Push.onNotificationOpened(
myNotificationOpenedHandler,
);
Notifications.Push.getLaunchNotification().then(notification => {
console.log('getLaunchNotification', notification);
// Take further action if needed
});
handlePermissions().then(result => {
console.log('handlePermissions', result);
});
useEffect(() => {
return () => {
console.log('clear listener');
tokenReceivedHandlerEventListener.remove();
notificationReceivedInForegroundListener.remove();
notificationReceivedInBackgroundListener.remove();
notificationOpenedListener.remove();
};
}, [
notificationOpenedListener,
notificationReceivedInBackgroundListener,
notificationReceivedInForegroundListener,
tokenReceivedHandlerEventListener,
]);
return (
<AuthProvider>
<Navigation />
</AuthProvider>
);
}
I see the [document](https://docs.amplify.aws/lib/push-notifications/interact-with-notifications/q/platform/react-native/#onnotificationopened), it said:
> Add onNotificationOpened listeners to respond to a push notification being opened while your app is in a foreground or background state.
But when my app in foreground (active) state, onNotificationOpened is not call.
Not sure if it can help, but react-native-splash-screen library was preventing Notifications.Push.onNotificationOpened
and Notifications.Push.getLaunchNotification
from being triggered when a push notification was pressed.
Without that library it works fine... now gotta find an alternative to the splash screen 😮💨
Yeah, @MensurRasic try to check this library: react-native-bootsplash
@tyndria, @aalindQuantel, or @banv, are any of you still experiencing issues with this that seem to be outside of the expected default behavior for onNotificationReceivedInForeground()
? If not we'll close this issue, but I don't think there appears to be any bugs with this at this time.
@cwomack I wasn't working closely with push notifications, but I think it could be closed if there are no other bugs thanks for you help!
@tyndria, appreciate the follow up and we can reopen if needed. I'll close the issue for now. Thanks!
Before opening, please confirm:
JavaScript Framework
React Native
Amplify APIs
Authentication, GraphQL API, Push Notifications
Amplify Categories
auth, function, api, notifications
Environment information
Describe the bug
onNotificationReceivedInForeground
is triggered not only for Push notifications received in foreground iOS app. But also when:onNotificationOpened
also works)Notifications.Push.getLaunchNotification
also works)So I only need
onNotificationReceivedInForeground
if app is foreground, moreover, schemas in documentation doesn't contains trigger of this listener for background or terminated states.Is it expected that
onNotificationReceivedInForeground
triggers always?Expected behavior
onNotificationReceivedInForeground
triggers only when push notification comes when app is in foreground modeReproduction steps
Test messaging
featureaws-exports.js
Manual configuration
No response
Additional configuration
AppDelegate.h
AppDelegate.mm
Podfile
index.js
App.tsx
Mobile Device
iPhone 14
Mobile Operating System
iOS 16.4.1
Additional information and screenshots
This video presents the following case:
getLaunchNotification
is working because the first screen is a screen opened by deep link urlonNotificationReceivedInForeground
triggers as well, because inner notification is visiblehttps://user-images.githubusercontent.com/17138916/236154585-3249b6b6-8249-4688-9a18-3db22fead140.mov