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:
<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:
[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:
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.