Xamarin Forms local notifications

856 views Asked by At

I'm trying to make an app that schedules different activities like walking and running with Xamarin Forms. I spent a few days trying to make it work, but I just couldn't find the correct way to do it.

So I want an app to be running on the background and at certain times to show notification that it's time to do some exercise. It's pretty trivial. I was able to schedule notifications with Xamarin cross platforms plugins. But when user clicks on notification, it takes him to the main page again. Even though I navigating to the exercise page prior to showing notification. So the best way is to control what shows up when user clicks the notification. I found an Android-specific example of showing notification and redirecting user to the second activity within Android project. I couldn't find a way to adapt this code for Xamarin forms app.

What I didn't try yet is to create exercise page within android project and try to open my Forms exercise page from there. But I'm pretty sure that there is a better way to deal with it without creating unnecessary class.

I'm not asking for a specific code, but the correct implementation with some tips or instructions. Either I need to implement notifications for each platform by myself and work from there or to use cross-platform plugin to show notifications and somehow redirect user to the correct page within Forms. The second one seems easier, but I still don't know how it's done.

tl;dr a correct way to redirect user clicking notification to second activity within Xamarin Forms

public void ShowNotification()
        {
        var valuesForActivity = new Bundle();
        valuesForActivity.PutInt(COUNT_KEY, count);

        // When the user clicks the notification, SecondActivity will start up.
        var resultIntent = new Intent(this, typeof(exerciseWalking));

        // Pass some values to SecondActivity:
        resultIntent.PutExtras(valuesForActivity);

        // Construct a back stack for cross-task navigation:
        var stackBuilder = TaskStackBuilder.Create(this);
        stackBuilder.AddParentStack(Class.FromType(typeof(exerciseWalking)));
        stackBuilder.AddNextIntent(resultIntent);

        // Create the PendingIntent with the back stack:
        var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

        // Build the notification:
        var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                      .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                      .SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
                      .SetContentTitle("Button Clicked") // Set the title
                      .SetNumber(count) // Display the count in the Content Info
                      //.SetSmallIcon(Resource.Drawable.icon) // This is the icon to display
                      .SetContentText($"The button has been clicked {count} times."); // the message to display.

        // Finally, publish the notification:
        var notificationManager = NotificationManagerCompat.From(this);
        notificationManager.Notify(NOTIFICATION_ID, builder.Build());

        // Increment the button press count:
        count++;
    }

This is code from one of the examples. In line 14, with stackBuilder.AddParentStack, it won't let me use my Forms content page. It needs an android activity. But I don't want to create one just to open Forms page. Which I'm not sure even possible. Thanks.

0

There are 0 answers