Local Notification shows blank page over the lauched activity's page

catalinapcatalinap ROMember ✭✭
edited January 2018 in Xamarin.Android

Hello,

I am developing a local notifications service in Xamarin with Visual Studio, the tap on the notification is supposed to open a page in the app containing the Inbox-like list of the messages received, with the possibility to go back to the app by pressing Back.

There is a service that reads the messages and launches the notification like this:

        Bundle valuesForActivity = new Bundle();
                valuesForActivity.PutInt( "count", unreadAlertsCount );

                // When the user clicks the notification, SecondActivity will start up.
                Intent resultIntent = new Intent( Android.App.Application.Context, typeof( NotifListActivity ) );
                //resultIntent.AddFlags( ActivityFlags.SingleTop );// | ActivityFlags.ClearTask );

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

                TaskStackBuilder stackBuilder = TaskStackBuilder.Create( Android.App.Application.Context );
                stackBuilder.AddParentStack( Java.Lang.Class.FromType( typeof( NotifListActivity ) ) );
                stackBuilder.AddNextIntent( resultIntent );
                var pendingIntent = stackBuilder.GetPendingIntent( 0, (int)PendingIntentFlags.UpdateCurrent );

                // Build the notification:
                NotificationCompat.Builder builder = new NotificationCompat.Builder( Android.App.Application.Context )
                    .SetAutoCancel( true )                    // Dismiss from the notif. area when clicked
                    .SetContentIntent( pendingIntent )  // Start 2nd activity when the intent is clicked.
                    .SetContentTitle( "Alerte necitite!!" )      // Set its title
                    .SetNumber( unreadAlertsCount )           // Display the count in the Content Info
                    .SetSmallIcon( Resource.Drawable.ic_bluetooth_white )  // Display this icon
                    .SetPriority( (int)NotificationPriority.High )
                    .SetContentText( String.Format(
                        "Exista {0} notificari necitite.", unreadAlertsCount ) ); // The message to display.

                //Finally, publish the notification:
                NotificationManager notificationManager = (NotificationManager)Forms.Context.GetSystemService( Context.NotificationService );
                notificationManager.Notify( ButtonClickNotificationId, builder.Build() );

For the Second Activity to be lauched when the notification is tapped:

[Activity( MainLauncher = false )]
[IntentFilter( new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryEmbed } ),]

public class NotifListActivity : Activity
{
    protected override void OnCreate( Bundle savedInstanceState )
    {
        base.OnCreate( savedInstanceState );

        Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync( new Common.Views.MessagesListView () );

        ////alternative solution that works in the same way, 
        ////where MenuView is the Navigation page that listens for the "show data" message 
        ////and does GetNavigationPage.PushAsync( new MessagesListView (), true ) when message is received
        //MenuView currentPage = (MenuView)( Xamarin.Forms.Application.Current.MainPage ):
        //MessagingCenter.Send<MenuView, string>( currentPage, "show data", "" );
}
} 

where

  • the NotifListActivity has defined as PARENT_ACTIVITY the MainActivity, so that pressing Back takes you Back to Main, and that works fine!
  • and MessagesListView is a ContentPage with the list of the Messages recieved, in an Inbox style

The solution DOES lauch the NotifListActivity, and it DOES draw the layout of the list (breakpoints are reached), but the screen is blank :(
The only time ou CAN see the Messegas list is when the phone gets lockscreen, you double-tap the notification, the same blank screen in shown,
but when pressing BACK, the messages list is shown :) , then the second BACK taes you back to the app as it's supposed.
Also the first Back removes the blank page to the right, while the second Back removes the Messages List to UP. Have no idea why.

So, I suspect there is a blank page OVER the messages list, does anyone know why it appears or how to make le messages list show in the first place?

Best Answer

  • catalinapcatalinap RO ✭✭
    Accepted Answer

    Hello, I have solved the problem the same day, I simply wrote

            OnBackPressed();
    

    after the PushModalAsync, and all is fine now.
    Also I have found there is a simpler way to get the needed pendingIntent, without the TaskStackBuilder, wich maybe unpredictable (they work differently under Android.Support.V4 or Android.Support.V7).
    Simply:
    PendingIntent resultPendingIntent = PendingIntent.GetActivity( Android.App.Application.Context, 0, resultIntent, PendingIntentFlags.UpdateCurrent );

    Greetings!

Answers

  • catalinapcatalinap ROMember ✭✭
    Accepted Answer

    Hello, I have solved the problem the same day, I simply wrote

            OnBackPressed();
    

    after the PushModalAsync, and all is fine now.
    Also I have found there is a simpler way to get the needed pendingIntent, without the TaskStackBuilder, wich maybe unpredictable (they work differently under Android.Support.V4 or Android.Support.V7).
    Simply:
    PendingIntent resultPendingIntent = PendingIntent.GetActivity( Android.App.Application.Context, 0, resultIntent, PendingIntentFlags.UpdateCurrent );

    Greetings!

Sign In or Register to comment.