I get an exception when I receive a push notification on my Android tablet. Java.Lang.IllegalArgumentException has been thrown

Kim Strasser 1,321 Reputation points
2023-02-13T10:14:42.86+00:00

I want to receive push notifications on my Samsung tablet, but I get this exception when I want to display a notification:

Java.Lang.IllegalArgumentException has been thrown com.name.myproject: 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.

I get the exception in this line:

var pendingIntent = PendingIntent.GetActivity(this, Activity1.NOTIFICATION_ID, intent, PendingIntentFlags.OneShot);

What should I change in my code?

MyFirebaseMessagingService.cs:

using System;
using Android.App;
using Android.Content;
using Android.Util;
using Firebase.Messaging;
using System.Collections.Generic;
using AndroidX.Preference;
using Android.Media;
using AndroidX.Core.App;

namespace MyAndroidProject
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";

        public override void OnNewToken(string token)
        {
            Log.Debug(TAG, "Refreshed token: " + token);
            storeToken(token);
        }

        private void storeToken(String token)
        {
            //saving the token on shared preferences
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
            ISharedPreferencesEditor editor = prefs.Edit();
            editor.PutString("my_token", token);
            editor.Apply();
        }

        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.Debug(TAG, "From: " + message.From);
            var body = message.GetNotification().Body;
            var title = message.GetNotification().Title;
            Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
            SendNotification(body, title, message.Data);
        }

        void SendNotification(string messageBody, string Title, IDictionary<string, string> data)
        {
            var intent = new Intent(this, typeof(Activity1));
            intent.AddFlags(ActivityFlags.ClearTop);
            foreach (var key in data.Keys)
            {
                intent.PutExtra(key, data[key]);
            }

            var pendingIntent = PendingIntent.GetActivity(this,
                                                          Activity1.NOTIFICATION_ID,
                                                          intent,
                                                          PendingIntentFlags.OneShot);

            var notificationBuilder = new NotificationCompat.Builder(this, Activity1.CHANNEL_ID)
                                      .SetSmallIcon(Resource.Mipmap.Icon)
                                      .SetContentTitle(Title)
                                      .SetContentText(messageBody)
                                      .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                                      .SetVibrate(new long[] { 1000, 1000, 0, 0, 0 })
                                      .SetLights(Android.Graphics.Color.Red, 3000, 3000)
                                      .SetPriority((int)NotificationPriority.High)
                                      .SetAutoCancel(true)
                                      .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(Activity1.NOTIFICATION_ID, notificationBuilder.Build());
        }
    }
}
Developer technologies | .NET | Xamarin
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-02-14T01:32:58.12+00:00

    Hello,

    Based on your description, please change the PendingIntentFlags from PendingIntentFlags.OneShot to PendingIntentFlags.Immutable like following code.

    var pendingIntent = PendingIntent.GetActivity(this, Activity1.NOTIFICATION_ID, intent, PendingIntentFlags.Immutable);
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

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.