Forum Xamarin Xamarin.Forms

How to open a view after clicking on a notification

alex2a7alex2a7 Member ✭✭

Hello,
I started development under XAMARIN for 2 months and I can't seem to do the following:
I have a crossPlateform application with a side menu. Each line corresponds to a different view containing each of the webView. I also implemented "Azure notification Hub" to manage notifications on both platforms. The problem is that I would like to be able to open on a view which changes according to a key word defined in the payload.

Here is an example of the side menu that I have created :

All the views are created in the common project but the notifications are managed in the project of each platform. How can I do, for example from the android project, open on the Traficolor view after clicking on the notification since the views are not defined in this project?

Best Answers

  • alex2a7alex2a7 Member ✭✭
    Accepted Answer

    Finaly I find the solution :

    NavigationPage np = ((NavigationPage)((MasterDetailPage)App.Current.MainPage).Detail);
    np.PushAsync(new TraficolorPage());
    
    

    it works perfectly

  • alex2a7alex2a7 Member ✭✭
    edited February 20 Accepted Answer

    I add also the solution for iOS :

    Xamarin.Forms.NavigationPage np = ((Xamarin.Forms.NavigationPage) ((Xamarin.Forms.MasterDetailPage)App.Current.MainPage).Detail);
    
    np.PushAsync(new TraficolorPage());
    
    

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Create a class to inherit FirebaseMessagingService to handle messages and override the OnMessageReceived event to send local notifications. Start an intent in SendNotification method and override OnNewIntent event to achieve the navigation in MainActivity class.

    Custom FirebaseMessagingService class.

    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);
    
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    
        var notficiationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                                ...
                                .SetContentIntent(pendingIntent);
    
        var notificationManager = NotificationManager.FromContext(this);
        var localNotification = notficiationBuilder.Build();
        localNotification.Defaults |= NotificationDefaults.Vibrate;
        notificationManager.Notify(NOTIFICATION_ID, localNotification);
    }
    

    MainActivity class

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        ...
        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
    
            NotificationClickedEvent();
        }
        ...
    }
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/local-notifications#starting-an-activity-from-a-notification

  • alex2a7alex2a7 Member ✭✭

    Hello,
    The problem is not only that. I have already managed to open an activity by clicking on the notification but it must be created in the folder of each platform. I'm going to rephrase, maybe I misspoke.

    All views are in the Forms folder that contains a class named MainPage who is the master of architechture masterDetailPage.
    Then I have a page for each view (Traficolor, Bus, WebCam) in this same folder.

    Sometimes the application opens the Traficolor view and others the Bus view. It depends on what I will send in the notification, but these views do not exist in the Android or iOS version.

    This is my architechture :

    +---CAPAMOVEV3
    |   |   App.xaml
    |   |   App.xaml.cs
    |   |   AssemblyInfo.cs
    |   |   CAPAMOVEV3.csproj
    |   +---bin              
    |   +---Models
    |   |       HomeMenuItem.cs
    |   |       Item.cs     
    |   +---obj       
    |   +---Services
    |   |       IDataStore.cs
    |   |       MockDataStore.cs    
    |   +---ViewModels
    |   |       AboutViewModel.cs
    |   |       BaseViewModel.cs
    |   |       BusViewModel.cs
    |   |       CamViewModel.cs
    |   |       HybridWebView.cs
    |   |       ItemDetailViewModel.cs
    |   |       ItemsViewModel.cs
    |   |       TraficolorViewModel.cs 
    |   +---Views
    |   |       AboutPage.xaml
    |   |       AboutPage.xaml.cs
    |   |       CamPage.xaml
    |   |       CamPage.xaml.cs
    |   |       ItemDetailPage.xaml
    |   |       ItemDetailPage.xaml.cs
    |   |       ItemsPage.xaml
    |   |       ItemsPage.xaml.cs
    |   |       MainPage.xaml
    |   |       MainPage.xaml.cs
    |   |       MenuPage.xaml
    |   |       MenuPage.xaml.cs
    |   |       NewItemPage.xaml
    |   |       NewItemPage.xaml.cs
    |   |       TraficolorPage.xaml **_//PAGE I WANT TO CALL_**
    |   |       TraficolorPage.xaml.cs 
    |   \---ViewWeb
    |           TraficolorWebView.cs
    |           WebCamWebView.cs  
    +---CAPAMOVEV3.Android
    |   |   CAPAMOVEV3.Android.csproj
    |   |   CAPAMOVEV3.Android.csproj.user
    |   |   Constants.cs
    |   |   google-services.json
    |   |   MainActivity.cs **_//FILE WHERE I WANT TO CALL THE PAGE_**
    |   |   MyFirebaseMessagingService.cs
    |   +---Assets
    |   |       AboutAssets.txt 
    |   +---bin
    |   +---obj                     
    |   +---Properties
    |   |       AndroidManifest.xml
    |   |       AssemblyInfo.cs 
    |   +---Renderer
    |   |       MyWebClient.cs
    |   |       TraficolorViewRender.cs
    |   |       WebCamRenderer.cs
    |   \---Resources
    \---CAPAMOVEV3.iOS
        |   AppDelegate.cs
        |   CAPAMOVEV3.iOS.csproj
        |   Entitlements.plist
        |   Info.plist
        |   Main.cs 
        +---Assets.xcassets
        +---bin
        +---obj
        +---Properties
        \---Resources
    
    

    So, I don't understand how you can call these views on the folder of each platform (namespace.Droid or namespace.iOS).

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited February 18

    call these views on the folder of each platform (namespace.Droid or namespace.iOS).

    The Xamarin.Forms MessagingCenter class implements the publish-subscribe pattern, allowing message-based communication between components that are inconvenient to link by object and type references. This mechanism allows publishers and subscribers to communicate without having a reference to each other.

    Try to use MessagingCenter to navigate to a page from platform projects.

    page.xaml.cs

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
    
            MessagingCenter.Subscribe<object>(this, "Hi", (sender) =>
            {
                Navigation.PushAsync(new Page2());
            });
        }
    }
    

    MainActivity class

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        ...
        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
    
            NotificationClickedEvent();
        }
    
        void NotificationClickedEvent()
        {
            MessagingCenter.Send<object>(this, "Hi");
        }
    }
    
  • alex2a7alex2a7 Member ✭✭

    I put this code in my page.xml

    MessagingCenter.Subscribe<object>(this, "Traficolor", (sender) =>
                {
                    Navigation.PushAsync(new TraficolorPage());
                });
    

    And this in MainActivity.cs:

    MessagingCenter.Send<object>(this, "Traficolor");
    
    

    This opens the main view but not on my Traficolor view.

    I also try to do this :

    App.Current.MainPage =  new NavigationPage(new TraficolorPage());
    

    It only opens the Traficolor page but not the complete architecture of the masterDetailPage with the Traficolor page open

  • alex2a7alex2a7 Member ✭✭
    Accepted Answer

    Finaly I find the solution :

    NavigationPage np = ((NavigationPage)((MasterDetailPage)App.Current.MainPage).Detail);
    np.PushAsync(new TraficolorPage());
    
    

    it works perfectly

  • JarvanJarvan Member, Xamarin Team Xamurai

    Congrats! If you've solved the issue, please mark the helpful reply as the answer. It'll help others who face the similar problem.

  • alex2a7alex2a7 Member ✭✭
    Thanks for your help. Your answer set me on the way.
  • alex2a7alex2a7 Member ✭✭
    edited February 20 Accepted Answer

    I add also the solution for iOS :

    Xamarin.Forms.NavigationPage np = ((Xamarin.Forms.NavigationPage) ((Xamarin.Forms.MasterDetailPage)App.Current.MainPage).Detail);
    
    np.PushAsync(new TraficolorPage());
    
    
Sign In or Register to comment.