Issue with Azure Push Notifications on Xamarin Android - PendingIntent Error

Mammad Mammadov 0 Reputation points
2023-11-24T12:15:06.1166667+00:00

I've implemented push notifications for my Xamarin application, and everything works smoothly on iOS. However, when the Android application is put in the background or closed, notifications fail to reach the device, and an error is encountered:

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE; only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g., 'if it needs to be used with inline replies or bubbles.'

My app's version is 33, Microsoft.Azure.NotificationHubs version is 4.1.0, and Xamarin.Forms version is 5.0.0.2337. Here's a snippet of my code:

public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity));
intent.PutExtra(TitleKey, message.Title);
intent.PutExtra(MessageKey, message.Body);
PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId++, intent, PendingIntentFlags.Immutable);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
    .SetContentIntent(pendingIntent)
    .SetContentTitle(message.Title)
    .SetContentText(message.Body)
    .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.abc_btn_check_material))
    .SetSmallIcon(Resource.Drawable.ateshgah_loading)
    .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
    .SetPriority(NotificationCompat.PriorityHigh);
builder.SetFullScreenIntent(pendingIntent, true);
Notification notification = builder.Build();
manager.Notify(messageId++, notification);
}

I would appreciate any insights or suggestions on resolving this issue.

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.
323 questions
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
Microsoft Intune Android
Microsoft Intune Android
Microsoft Intune: A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.Android: An open-source mobile platform based on the Linux kernel, developed by Google, and maintained by the Open Handset Alliance.
299 questions
{count} votes

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.