Azure Notification Hub – Firebase – MAUI – Notifications when app is closed – Android

I am learning to use azure notifications hub and firebase messanging for a different project. I have successfully implemented a notification setup that can receive push notifications if the app is open or minimized.
The current issue is, when the app is closed, no notifications get received, even after a while as ive read it could take some time.

I have set up a receiver in my androidmanifest.xml as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
android:exported="true" />
<receiver
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
</code>
<code><receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="true" /> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <action android:name="com.google.firebase.MESSAGING_EVENT" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> </code>
<receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
    android:exported="true" />
<receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
        <category android:name="${applicationId}" />
    </intent-filter>
</receiver>

From what I understand that should be enough for it to be received when closed, as the FirebaseMessagingService’s OnMessageReceived Only gets called when the app is open, I have also added a service tag on this class, and will share it here for more clarity:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[Service(Exported = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class PushNotificationFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage msg)
{
base.OnMessageReceived(msg);
var receivedNotification = msg.GetNotification();
var title = receivedNotification.Title;
var body = receivedNotification.Body;
var data = msg.Data;
ShowNotification(title, body);
}
private void ShowNotification(string title, string body)
{
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
var notificationChannel = new NotificationChannel("YOUR_CHANNEL_ID", "YOUR_CHANNEL_NAME", NotificationImportance.Default);
notificationManager.CreateNotificationChannel(notificationChannel);
var notificationBuilder = new Notification.Builder(this, "YOUR_CHANNEL_ID")
.SetContentTitle(title)
.SetContentText(body)
.SetAutoCancel(true)
.SetSmallIcon(_Microsoft.Android.Resource.Designer.ResourceConstant.Drawable.ic_call_answer_low);
var notification = notificationBuilder.Build();
notificationManager.Notify(0, notification);
}
}
</code>
<code>[Service(Exported = true)] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class PushNotificationFirebaseMessagingService : FirebaseMessagingService { public override void OnMessageReceived(RemoteMessage msg) { base.OnMessageReceived(msg); var receivedNotification = msg.GetNotification(); var title = receivedNotification.Title; var body = receivedNotification.Body; var data = msg.Data; ShowNotification(title, body); } private void ShowNotification(string title, string body) { var notificationManager = (NotificationManager)GetSystemService(NotificationService); var notificationChannel = new NotificationChannel("YOUR_CHANNEL_ID", "YOUR_CHANNEL_NAME", NotificationImportance.Default); notificationManager.CreateNotificationChannel(notificationChannel); var notificationBuilder = new Notification.Builder(this, "YOUR_CHANNEL_ID") .SetContentTitle(title) .SetContentText(body) .SetAutoCancel(true) .SetSmallIcon(_Microsoft.Android.Resource.Designer.ResourceConstant.Drawable.ic_call_answer_low); var notification = notificationBuilder.Build(); notificationManager.Notify(0, notification); } } </code>
[Service(Exported = true)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class PushNotificationFirebaseMessagingService : FirebaseMessagingService
{
    public override void OnMessageReceived(RemoteMessage msg)
    {
        base.OnMessageReceived(msg);

        var receivedNotification = msg.GetNotification();

        var title = receivedNotification.Title;
        var body = receivedNotification.Body;
        var data = msg.Data;

        ShowNotification(title, body);
    }

    private void ShowNotification(string title, string body)
    {
        var notificationManager = (NotificationManager)GetSystemService(NotificationService);

        var notificationChannel = new NotificationChannel("YOUR_CHANNEL_ID", "YOUR_CHANNEL_NAME", NotificationImportance.Default);
        notificationManager.CreateNotificationChannel(notificationChannel);

        var notificationBuilder = new Notification.Builder(this, "YOUR_CHANNEL_ID")
            .SetContentTitle(title)
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetSmallIcon(_Microsoft.Android.Resource.Designer.ResourceConstant.Drawable.ic_call_answer_low);

        var notification = notificationBuilder.Build();
        notificationManager.Notify(0, notification);
    }
}

Other than those two, i cant think waht would be needed extra for the background notifications. I register my device with the azure notification hub and then use the “Test Send” for testing it out. As I understand, if it contains a notification part, it will be handled by the google play services and will be shown in the notifications tab, but it doesnt.

For more clarity, find below the registration for the device on azure notification hub I use:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static async Task RegisterDeviceAzureSDK(string notificationHubNamespace, string notificationHub, string key)
{
if (!NotificationsSupported)
return;
try
{
NotificationHubClient hubClient = new NotificationHubClient(
connectionString: "{My endpoint string is set here}",
notificationHubPath: "app");
var firebaseToken = await FirebaseMessaging.Instance.GetToken();
var deviceId = GetDeviceId();
var installation = new FcmV1Installation(deviceId, firebaseToken.ToString());
await hubClient.CreateOrUpdateInstallationAsync(installation);
}
catch (Exception ex)
{
Console.WriteLine("[Android] Push Notification Registration Error " + ex);
}
}
</code>
<code>public static async Task RegisterDeviceAzureSDK(string notificationHubNamespace, string notificationHub, string key) { if (!NotificationsSupported) return; try { NotificationHubClient hubClient = new NotificationHubClient( connectionString: "{My endpoint string is set here}", notificationHubPath: "app"); var firebaseToken = await FirebaseMessaging.Instance.GetToken(); var deviceId = GetDeviceId(); var installation = new FcmV1Installation(deviceId, firebaseToken.ToString()); await hubClient.CreateOrUpdateInstallationAsync(installation); } catch (Exception ex) { Console.WriteLine("[Android] Push Notification Registration Error " + ex); } } </code>
public static async Task RegisterDeviceAzureSDK(string notificationHubNamespace, string notificationHub, string key)
{
    if (!NotificationsSupported)
        return;

    try
    {
        NotificationHubClient hubClient = new NotificationHubClient(
            connectionString: "{My endpoint string is set here}",
            notificationHubPath: "app");

        var firebaseToken = await FirebaseMessaging.Instance.GetToken(); 
        var deviceId = GetDeviceId();

        var installation = new FcmV1Installation(deviceId, firebaseToken.ToString());
        
        await hubClient.CreateOrUpdateInstallationAsync(installation);
    }
    catch (Exception ex)
    {
        Console.WriteLine("[Android] Push Notification Registration Error " + ex);
    }
}

Any help on this will be greatly appreciated!
summary is I want a notification sent by Azure notification hub to be received and displayed in the android notification dropdown.

I feel like i am missing one small thing, but just cant figure out what.

I tried adding different services in the androidmanifest, expecting it to be the issue, but the varients I tried dont work. I am not too familiar with these services and receivers. I simply want the notification to display with a title and a body.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật