Open Specific Page when click on Push Notification

PhaniVPhaniV Member ✭✭

Hi,
I am trying implement basic push notification example using Xamarin Forms with Prism MVVM, Azure & FCM.

I am receiving notification, but couldn't navigate to specific page when clicked on notification.

Trying basic functionality when app is running or in background (not closed).
It's throwing exception "PushAsync not supported globally on Android, please use a NavigationPage" at

ExploreXam.App.Current.MainPage.Navigation.PushAsync(page);

Here are MainActivity.cs and FireBaseMessagingService & App classes:

[Activity(LaunchMode = LaunchMode.SingleTask, MainLauncher = true]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    internal static readonly string CHANNEL_ID = "explore_xamarin";
    internal static readonly int NOTIFICATION_ID = 1029;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        CreateNotificationChannel();
        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }
    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        Intent = intent;
        NotificationClickedOn(intent);
    }
    private void NotificationClickedOn(Intent intent)
    {
        if (intent.Action == ExploreXamFirebaseMessagingService.ExploreXamNotification && intent.HasExtra("XamId"))
        {
            var page = new Xamarin.Forms.NavigationPage(new SpecificPage());
            Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(page);
            ExploreXam.App.Current.MainPage.Navigation.PushAsync(page);
        }
    }
}
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class ExploreXamFirebaseMessagingService : FirebaseMessagingService
{
    private const string TAG = "ExploreXamFirebaseMsgService";
    public const string ExploreXamNotification = "ExploreXamNotification";

    public override void OnMessageReceived(RemoteMessage message)
    {
        SendNotification(message);
    }

    private void SendNotification(RemoteMessage message)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.SingleTop);
        intent.AddFlags(ActivityFlags.NewTask);

        intent.SetAction(ExploreXamNotification);

        intent.PutExtra("XamId", message.Data["XamId"]);

        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
        var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

        var notficiationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                                                        .SetContentTitle(message.Data["title"] ?? "Explore Xam")
                                                        .SetSmallIcon(Resource.Drawable.Explore_Xam_Icon)
                                                        .SetContentText(message.Data["body"] ?? "You have a new message")
                                                        .SetAutoCancel(true)
                                                        .SetNumber(Int32.Parse(message.Data["badge"]))
                                                        .SetSound(defaultSoundUri)
                                                        .SetContentIntent(pendingIntent)
                                                        .SetSubText(message.Data["subtitle"] ?? "");

        var notificationManager = NotificationManager.FromContext(this);
        var localNotification = notficiationBuilder.Build();
        localNotification.Defaults |= NotificationDefaults.Vibrate;
        notificationManager.Notify(NOTIFICATION_ID, localNotification);
    }
}

public partial class App : PrismApplication
{
    public bool navigating;

    public App(IPlatformInitializer initializer = null, bool shallNavigate=false) : base(initializer)
    {
        navigating = shallNavigate;
    }
    protected async override void OnInitialized()
    {
        BlobCache.ApplicationName = "ExploreXam";
        InitializeComponent();
        FlowListView.Init();
        //await  NavigationService.NavigateAsync("LoginPage");
        await NavigationService.NavigateAsync("NavigationPage/LoginPage");
    }
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        //mapping 
    }
}

Any help please....!

Sign In or Register to comment.