2

I recently updated the nugget packages in my Android project and now I get a message in this line of code:

 ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);

CS0618: PreferenceManager.GetDefaultSharedPreferences(Context?) is obsolete: deprecated

enter image description here

Is it necessary to change something? Will my code not work correctly like this? Should I completely remove private void storeToken(String token)? I'm not sure if I still need private void storeToken(String token).

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

    namespace AndroidVersion
    {
    [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.Drawable.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());
        }
    }
}
Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
  • Just replace this line: prefs = PreferenceManager.getDefaultSharedPreferences(context) by this one prefs = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE) – Mahmoud Zaher Jul 12 '22 at 13:25

2 Answers2

0

I use implementation 'androidx.preference:preference-ktx:1.1.1' successfully replaced the deprecated android.preference.PreferenceManager in my project

Galih Al
  • 41
  • 2
-1

Use using AndroidX.Preference; instead of using Android.Preferences;

Ashton Yoon
  • 172
  • 2
  • 6