Xamarin Forms Android Push Notifications not received when app has been swiped closed

Melissa Warrington 6 Reputation points
2021-03-15T17:16:31.943+00:00

Hello,

I have been updating my Xamarin Forms app to receive Firebase push notifications using the following tutorial: https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm but I am having some problems and the code example currently attached to the documentation has not been updated so I can not use it to test.

The main problems I have encountered are:

  1. Unless I manually create a notification channel via the 'OnCreate' method when the app is launched, then no notifications are displayed in the devices notification tray. The 'OnPushNotificationReceived' method is triggered however, but if the user doesn't see the notification coming through that is a bit pointless. If it is necessary to create a channel manually this should be included in the documentation.
  2. The biggest problem I am having is on receiving push notifications when the app is swiped closed (not force shut). The app will happily receive notifications when it is running in the foreground or background but when I try to send a notification when the app is closed I get the following error message (found in the device log): "Error (6621) / AndroidRuntime: java.lang.NullPointerException: Attempt to invoke interface method 'void com.microsoft.windowsazure.messaging.notificationhubs.NotificationListener.onPushNotificationReceived(android.content.Context, com.microsoft.windowsazure.messaging.notificationhubs.NotificationMessage)' on a null object reference". What am I doing wrong here or what am I missing?

Final few things to mention are:

  • I previously had push notifications working perfectly using the older 'Legacy' configuration which included a FirebaseService class in the android project. The app received notifications in all states, foreground, background and closed, I tested this again today to be sure.
  • My android manifest file has all the necessary permissions that are provided in the tutorial linked above
  • All packages are up to date in the project

Many thanks,
Melissa

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
325 questions
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Martin Strobel 1 Reputation point Microsoft Employee
    2021-03-18T16:47:04.33+00:00

    Regarding channels, so long as your payload is not calling to deliver to a specific channel, you shouldn't need to create one. Though likewise, if you do create a channel you'll need to indicate which one you're targeting in your payload.

    To the other point, it sounds like the receiver that you've registered is getting garbage collected after the app is tomb-stoned. Out of curiosity, is the object you've set as a receiver also an Activity or Service? I could imagine those objects getting torn down and needing to be recreated by starting them and passing an intent.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.