Local notification - resume app on tap in Android

Hi
I'm creating a Xamarin Forms app and currently I'm struggling with Android implementation of local notification. What I want to achieve ist that when I tap the notification from my app it gets resumed just like the way it's done from home button and not start all over again. I've read many threads and tried many options but nothing works for me, here's my code:
`Intent intent = new Intent(Application.Context, typeof(MainActivity));
intent.SetFlags(ActivityFlags.ClearTop);

        const int pendingIntentId = 0;
        PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, pendingIntentId, intent, PendingIntentFlags.OneShot);

        // Instantiate the builder and set notification elements, including pending intent:
         Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

        Notification.Builder builder = new Notification.Builder(Application.Context).SetContentTitle(title).SetContentText(text).
            SetSmallIcon(Resource.Drawable.small).SetSound(uri).SetContentIntent(pendingIntent);


        // Build the notification:
        Notification notification = builder.Build();
        notification.Flags |= NotificationFlags.OngoingEvent;

        // Get the notification manager:
        NotificationManager notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;

        // Publish the notification:
        const int notificationId = 0;
        notificationManager.Notify(notificationId, notification);`

What am I doing wrong ? Maybe someone has got code for that and could share it ?

Greets,
Maciek

Answers

  • TonyDTonyD USMember ✭✭✭

    @MaciejGawel did you ever solve this? Can you share some code?

    I have the same problem where even if the app is already open, when I click on a push notification it gets reopened again

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Give your Activity the SingleTop LaunchMode, as shown here. This should make it open your application in the same Activity instead of starting it up again.

  • XnainaXnaina USMember ✭✭✭

    @TonyD try this for android local notifications:- https://github.com/XnainA/CustomLocalNotification

  • DonCB2BDonCB2B USMember ✭✭✭

    Does anyone know? I found it will do something with ActivityFlags.SingleTop, but i add this in flags, it still now working. Anyone know how exact it set? Moreover, how to add android:launchMode="singleInstance" on manifest file?

  • SuchithSuchith INMember ✭✭✭

    @AdamMeaney It looks like changing LaunchMode to SingleTop works!. Can u brief out like how does it works exactly, creating the single copy of the activity.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @Suchith I would say this is the best explanation, as it is straight from Google.

    Just scroll down to the launch mode explanation.

    When your application is already open, it receives new launch intents in the OnNewIntent method, which is where you should handle the notification.

  • returnZeroreturnZero Member

    Any one able to find a working solution? i face the same issue as well..

Sign In or Register to comment.