OnNotificationOpened not fired on Android when notification is tapped while app is closed. (Xamarin.Forms)
Vineland opened this issue · 4 comments
Been working on this problem on and off for a few days. I am receiving notifications OK while the app is closed/foreground/background and I've set it up so that depending on Data that is attached to the notification tapping on it will take the user to the relevant section/screen in the app. This works perfectly if the app is running whether that's the background or foreground but if the app is closed all that happens when you tap on a notification is that it opens the app and takes you to the main screen.
I've done some searching on this and have tried everything that I could find. I've moved my subscription to OnNotificationOpened from the MainActivity to the Application OnCreate method and I have my ProcessIntent calls in the correct places in the MainActivity (after LoadApplication OnCreate and also in OnNewIntent) but still not getting the behaviour I want. I did notice in the 'Getting Started' documentation that the example code for the Application has the following:
//Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived += (s,p) =>
{
};
I've not done the same in my app because:
a) I'm not entirely sure what I'd put in there
b) I don't want to do anything in particular when the Notification is received.
@Vineland
If you have SplashActivity as MainLauncher = true , try to move code from MainActiviy to your SplashActivity
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
FirebasePushNotificationManager.ProcessIntent(this, Intent);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
FirebasePushNotificationManager.ProcessIntent(this, Intent);
}
Sorry, I should have mentioned that I've already removed my SplashActivity (I didn't need it) so my MainActivity is MainLauncher=true.
I can't really add anything useful except that the documentation is contradicting the example code. It is stated (in some issues as well) that the MainLauncher should contain "FirebasePushNotificationManager.ProcessIntent(this, Intent);" however it does not work whether I place this in the SplashActivity (MainLauncher) or MainActivity.
I am also not sure if it is relevant WHERE the event is being processed (best test event is OnNotificationReceived). Currently I am trying it in the Xamarin Forms App.xaml.cs and in the MainApplication OnCreate() method. In the end I want this to work if the application is running, in background OR killed. However currently is is working with neither Notification nor Data messages.
Just to elaborate on the data I am sending:
"to": "Device-Token",
"priority": "high",
"notification" : {
"title": "New message",
"body" : "You received a new message.",
"icon": "app_icon",
"sound": "default",
"color": "#bb2231",
"click_action": "message",
"content_available" : true
}
This works if the application is in background. The Android FCM handler provides an notification. However opening the application on message click does not work at all. No event (including OnNotificationReceived) is triggered.
"to": "Device-Token",
"priority": "high",
"data" : {
"title": "New message",
"text" : "You received a new message.",
"icon": "app_icon",
"sound": "default",
"color": "#bb2231"
}
This does never shows and never triggers an event.
I think I found a solution IF the android target sdk is set to v31 (Android 12) and the device (in my case: emulator) is Android 12. If this is the case please refer to #420.