I have an application ( xamarin.android ), it accepts push messages. I want to display these messages in TextBox , but they are displayed only when the application is running in the foreground. How to make it accept messages, even when the application is not running?

My code is:

  [Service] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class MyFirebaseMessagingService : FirebaseMessagingService { const string TAG = "MyFirebaseMsgService"; //public override void OnMessageSent(string msgId) { } public override void OnMessageReceived(RemoteMessage message) { Log.Debug(TAG, "From: " + message.From); Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body); SendNotification(message.GetNotification().Body, message.Data); } public void SendNotification(string messageBody, IDictionary<string, string> data) { MainActivity.JsonWiew.Text = messageBody.Replace("\r\n", "");; var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); foreach (string key in data.Keys) { intent.PutExtra(key, data[key]); } var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); var notificationBuilder = new Notification.Builder(this) //.SetSmallIcon(Resource.Drawable.ic_stat_ic_notification) .SetContentTitle("FCM Message") .SetContentText(messageBody) .SetAutoCancel(true) .SetContentIntent(pendingIntent); var notificationManager = NotificationManager.FromContext(this); notificationManager.Notify(0, notificationBuilder.Build()); } } 
  • Thank you very much for the answer! But could you help me with how to do this in xamarin? - Vladimir
  • I added these lines to MainActivity: var intent = new Intent(this, typeof(FirebaseMessagingService)); StartService(intent); var intent = new Intent(this, typeof(FirebaseMessagingService)); StartService(intent); But it does not come out :( - Vladimir
  • @Vladimir where do you want to display a message when the application is not running? here is the implementation for Xamarin docs.microsoft.com/ru-ru/xamarin/android/app-fundamentals/… - Dev

0