1. Ensure the you have set LaunchMode.SingleTop
on your MainActivity
:
LaunchMode.SingleTop
1 [Activity(Label = "云销管家", 2 Icon = "@mipmap/ic_launcher", 3 Theme = "@style/MainTheme", 4 LaunchMode = LaunchMode.SingleTop, 5 ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 6 public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IBDLocationListener 7 {
In your MainActivity
(the FormsAppCompatActivity subclass) add a OnNewIntent
override:
OnNewIntent:
1 protected override void OnNewIntent(Intent intent) 2 { 3 base.OnNewIntent(intent); 4 NotificationClickedOn(intent); 5 }
Now you can check the intent.Action
/ intent.HasExtra
to determine if it is your notification that was send and thus process it. With Xamarin.Forms
the easiest would be to use MessagingCenter
to send a message that is subscribed to within your .NetStd/PCL Xamarin.Forms
code base.
NotificationClickedOn:
1 void NotificationClickedOn(Intent intent) 2 { 3 4 if (intent.Action == "LocalNotifierIntent1300" && intent.HasExtra("LocalNotification")) 5 { 6 var notificationMessage = intent.Extras.GetString("LocalNotification"); 7 var winnerToast = Toast.MakeText(this, $"{notificationMessage}.", ToastLength.Long); 8 winnerToast.SetGravity(Android.Views.GravityFlags.Center, 0, 0); 9 winnerToast.Show(); 10 } 11 }
Send notification example:
1 void SendNotifacation() 2 { 3 var title = "Winner, Winner, Chicken Dinner"; 4 var message = "You just won a million * reputation points"; 5 6 var intent = new Intent(BaseContext, typeof(MainActivity)); 7 intent.SetAction("ASushiNotification"); 8 intent.PutExtra("MessageFromSushiHangover", message); 9 var pending = PendingIntent.GetActivity(BaseContext, 0, intent, PendingIntentFlags.CancelCurrent); 10 11 using (var notificationManager = NotificationManager.FromContext(BaseContext)) 12 { 13 Notification notification; 14 if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O) 15 { 16 #pragma warning disable CS0618 // Type or member is obsolete 17 notification = new Notification.Builder(BaseContext) 18 .SetContentTitle(title) 19 .SetContentText(message) 20 .SetAutoCancel(true) 21 .SetSmallIcon(Resource.Drawable.icon) 22 .SetDefaults(NotificationDefaults.All) 23 .SetContentIntent(pending) 24 .Build(); 25 #pragma warning restore CS0618 // Type or member is obsolete 26 } 27 else 28 { 29 var myUrgentChannel = BaseContext.PackageName; 30 const string channelName = "Messages from SushiHangover"; 31 32 NotificationChannel channel; 33 channel = notificationManager.GetNotificationChannel(myUrgentChannel); 34 if (channel == null) 35 { 36 channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High); 37 channel.EnableVibration(true); 38 channel.EnableLights(true); 39 channel.SetSound( 40 RingtoneManager.GetDefaultUri(RingtoneType.Notification), 41 new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build() 42 ); 43 channel.LockscreenVisibility = NotificationVisibility.Public; 44 notificationManager.CreateNotificationChannel(channel); 45 } 46 channel?.Dispose(); 47 48 notification = new Notification.Builder(BaseContext) 49 .SetChannelId(myUrgentChannel) 50 .SetContentTitle(title) 51 .SetContentText(message) 52 .SetAutoCancel(true) 53 .SetSmallIcon(Resource.Drawable.icon) 54 .SetContentIntent(pending) 55 .Build(); 56 } 57 notificationManager.Notify(1331, notification); 58 notification.Dispose(); 59 } 60 }