Launching application from android local notification

Hello,

I'm trying to launch the application when the user taps a local notification. To accomplish this, we need to set the ContentIntent to the notification, and that start's the desired activity set at the intent.

Here's some code:

Intent mainActivityIntent = new Intent(context, typeof(MainActivity)); PendingIntent mainActivityPendingIntent = PendingIntent.GetActivity(context, 0, mainActivityIntent, PendingIntentFlags.NoCreate); Notification.Builder builder = new Notification.Builder(context) .SetContentTitle("Atenção").SetContentText(texto) .SetSmallIcon(Resource.Drawable.icon); Notification notification = builder.Build(); notification.ContentIntent = mainActivityPendingIntent;

The problem is, when the user taps, if the application is already loaded, a new instance of the application is created. I tried to re-use an instance of the Application but the same problem happens.

Could somebody help me?

Best Answer

Answers

  • MommMomm USMember ✭✭✭

    @GuilhermIkeda, if I'm not mistaken, you could set
    mainActivityIntent.AddFlags(ActivityFlags.SingleTop);

    If it doesn't work, use another ActivityFlags instead.

    Please let me know if it helps.

  • JohnHardmanJohnHardman GBUniversity mod

    I know this is an old thread, but I suspect SingleTask will be preferable to SingleTop for Xamarin.Forms apps.
    Using SingleTop, I can get what seem to be two views onto the same app using the Split Screen view available in recent Android versions. That allows all sorts of weird things to happen. SingleTask seems to prevent that.

Sign In or Register to comment.