[Xamarin.Forms Android]When click notification, how to open specify page?

aluzi liu 486 Reputation points
2022-10-24T10:05:31.257+00:00

My App use AppShell, contains two TabBar:

    <TabBar>  
        <ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />  
    </TabBar>  
      
    <TabBar>  
        <ShellContent Title="{x:Static resources:AppResources.Tab_Home}" Route="Main" Icon="{FontImage &#xe66c;, FontFamily={StaticResource iconfont},Color=OrangeRed}" ContentTemplate="{DataTemplate local:HomePage2}" />  
        <ShellContent Title="{x:Static resources:AppResources.Tab_Todo}" x:Name="ShellTodoPage" x:FieldModifier="Public" Icon="{FontImage &#xe658;, FontFamily={StaticResource iconfont}}" ContentTemplate="{DataTemplate local:TodoPage}" />  
    </TabBar>  
  

When app launch, LoginPage will show, after login success, I navigated to Home page, and in homepage, I send local notification with this code:

            Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity));  
            intent.PutExtra(TitleKey, title);  
            intent.PutExtra(MessageKey, message);  
  
            PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, pendingIntentId++, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);  
  
            NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)  
                //.SetContentIntent(pendingIntent)  
                .SetContentTitle(title)  
                .SetContentText(message)  
                .SetAutoCancel(true)  
                .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.jpush_notification_icon))  
                .SetSmallIcon(Resource.Drawable.jpush_notification_icon)  
                .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);  
  
            Notification notification = builder.Build();  
            manager.Notify(messageId++, notification);  
  

The notification sent success, But my question is, when I click the notification, it will open LoginPage again!?
How to make it just active the app from background instead of re-openping LoginPage?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-10-25T03:10:27.347+00:00

    Hello,

    When app launch, LoginPage will show, after login success, I navigated to Home page, and in homepage, I send local notification with this code:

    It is caused by you do not have to set the start page of the app in the notification method.

    Please refer to the following steps:

    Step1: Add a constructor in your App.xaml.cs file to set start page.

       public App(ContentPage page)   
       {  
           DependencyService.Get<INotificationManager>().Initialize();  
         
           MainPage = page;  
       }  
    

    Step2: Add LoadApplication(new App(new Page1())); into your CreateNotificationFromIntent method:

       void CreateNotificationFromIntent(Intent intent)   
       {  
           if (intent?.Extras != null)  
           {  
               LoadApplication(new App(new Page1()));  
               ...  
               DependencyService.Get<INotificationManager>().ReceiveNotification(title, message);  
           }  
       }  
    

    Then, you could find the Page1 would be opened after clicking the notification.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.