App Reload on Push Notification Click Xamarin Forms

I have implemented push notifications in my app and they work fine. The problem I have is that when I click them in the dropdown menu, they immediately reload the application. To counter this, I had an application creating a new instance of an activity. Now a new page opens, but when clicking on that new page, it has the same issue and loads the whole app again.

I am using Xamarin Forms with PCL, so its not pure Android. Is there a way to get a click event on a menu item to load a specific kind of page in PCL? Restarting the whole application looks like it's useless.

This is the class that generates the phone notification:

ForegroundMessages.cs:

 [Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]

public class ForegroundMessages: FirebaseMessagingService
{


    const string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        //Log.Debug(TAG, "From: " + message.From);
        //Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
        base.OnMessageReceived(message);
        SendNotification(message.GetNotification().Body);
    }

    private void SendNotification(string body)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
       // var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        Log.Debug(TAG, "Notification Message Body: " + body);

        // sends notification to view model
        MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase");

        Intent resultintent = new Intent(this,typeof(SecondActivity));

        TaskStackBuilder stackbuilder = TaskStackBuilder.Create(this);
        stackbuilder.AddParentStack(Java.Lang.Class.FromType(typeof(SecondActivity)));
        stackbuilder.AddNextIntent(resultintent);

        PendingIntent resultPendingIntent = stackbuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

        var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
        var notificationBuilder = new NotificationCompat.Builder(this)
            .SetSmallIcon(Resource.Drawable.icon)
            .SetContentTitle("Notification")
            .SetContentText(body)
            .SetAutoCancel(true)
            .SetSound(defaultSoundUri)
            .SetContentIntent(resultPendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }


}

      

and the Second Activity class:

  [Activity(Label = "Notifications")]
public class SecondActivity:Activity
{

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        System.Diagnostics.Debug.WriteLine("this worked");
    }

}

      

As stated, this works and gets the messages ok and when you click on them it loads the second activity, but when removed from the second activity page it just reloads the whole app again. It would be better if I could load the view page (.xaml or .xaml.cs) in the PCL instead of this.

Any ideas?

+3


source to share


1 answer


You can check my post that shows how I did it, I also open the forms page. The post is quite long, but there are basically a few things:

  • I do not use the second Activity

  • I am not adding a flag ClearTop

    toMainActivity

    Intent

  • I don't use TaskStackBuilder

    and instead just use PendingIntent.GetActivity

    to getPendingIntent

  • Then I'll specifically address the issue of reloading the app in the post by setting MainActivity

    LaunchMode

    to LaunchMode.SingleTop

    , overriding OnNewIntent

    to MainActivity

    , and then checking intent.HasExtras

    for the special string

    > you set in your method SendNotification

    to distinguish this Intent

    from some otherIntent

Let me know if you still have problems.

Edit:



private void SendNotification(string body)
{
    var intent = new Intent(this, typeof(MainActivity));
    intent.AddFlags(ActivityFlags.ClearTop);
   // var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    intent.PutExtra("SomeSpecialKey", "some special value");

    Log.Debug(TAG, "Notification Message Body: " + body);

    // sends notification to view model
    MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase");

    PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);

    var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
    var notificationBuilder = new NotificationCompat.Builder(this)
        .SetSmallIcon(Resource.Drawable.icon)
        .SetContentTitle("Notification")
        .SetContentText(body)
        .SetAutoCancel(true)
        .SetSound(defaultSoundUri)
        .SetContentIntent(pendingIntent);

    var notificationManager = NotificationManager.FromContext(this);
    notificationManager.Notify(0, notificationBuilder.Build());
}

      

Then in MainActivity:

[Activity(LaunchMode = LaunchMode.SingleTop, ....)]
public class MainActivity : FormsApplicationActivity {

    protected override void OnNewIntent(Intent intent) {
        if(intent.HasExtra("SomeSpecialKey")) {
            System.Diagnostics.Debug.WriteLine("\nIn MainActivity.OnNewIntent() - Intent Extras: " + intent.GetStringExtra("SomeSpecialKey"); + "\n");
        }

        base.OnNewIntent(intent);
    }

      

+2


source







All Articles