Reputation: 733
I have Notification Hub setup correctly with FCM and my Android app. The problem is when my app is in foreground the notification never shows but debugger catches on OnPushNotificationReceived so I know the setup is working. Also when the app is backgrounded or not running the notification pops up. I think it has to do with the code which I got from: https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm
Here is my code:
public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
var intent = new Intent(context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(context, MainActivity.CHANNEL_ID);
notificationBuilder.SetContentTitle(message.Title)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(message.Body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(context);
notificationManager.Notify(0, notificationBuilder.Build());
}
Any help would be much appreciated
Upvotes: 0
Views: 715
Reputation: 733
I found the issue to be because no NotificationChannel existed so I created it in the contrstructor:
public AzureListener()
{
var channel = new NotificationChannel(MainActivity.CHANNEL_ID, "General", NotificationImportance.Default);
var notificationManager = NotificationManager.FromContext(Application.Context);
notificationManager.CreateNotificationChannel(channel);
}
It worked after I added this.
Upvotes: 2