Переход на определенную страницу при нажатии push-уведомления - PullRequest
0 голосов
/ 24 мая 2018

Я работаю с формами ксамарина.В Xamarin Android я получал уведомление, когда приложение не в foregroud / backgroud (то есть убивало приложение).При нажатии на уведомление мне нужно перейти на конкретную страницу.

protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            string notificationMessage = intent.GetStringExtra(Constants.MESSAGE);
            string notificationThreadId = intent.GetStringExtra(Constants.MESSAGE_THREAD_ID);
            bool isFromNotificaion = true;
            PushNotificationLog notificationLog = new PushNotificationLog(notificationMessage, notificationThreadId, isFromNotificaion);
            ConferenceMobileApp.App app = new App(notificationLog);
            LoadApplication(app);
        }

OnNewIntent вызывается, когда приложение на переднем или заднем плане, а не когда приложение убито.

И мой код отправки уведомления ниже

void SendNotification(RemoteMessage message)
        {
            string messageBody = "";
            string messageThreadId = "";
            message.Data.TryGetValue(MESSAGE,out messageBody);
            message.Data.TryGetValue(MESSAGE_THREAD_ID, out messageThreadId);
            string messageLogId = "0";//message.Data.TryGetValue(MESSAGE_LOG_ID);
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            intent.AddFlags(ActivityFlags.SingleTop);
            intent.PutExtra(MESSAGE, messageBody);
            intent.PutExtra(MESSAGE_THREAD_ID, messageThreadId);
            //intent.PutExtra(MESSAGE_LOG_ID, messageLogId);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);


            var notificationBuilder = new Notification.Builder(this).SetSmallIcon(Resource.Drawable.common_google_signin_btn_icon_dark)
                .SetContentTitle("PKConf")
                .SetContentText(messageBody)
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);
            //setting notification id
            int notificaionId = Convert.ToInt32(messageLogId);
            notificationManager.Notify(notificaionId, notificationBuilder.Build());
        }

Как могЯ достигаю этого, когда приложение не на переднем плане / фоне?

Ответы [ 2 ]

0 голосов
/ 25 мая 2018

Пожалуйста, попробуйте с событиями ниже в вашем mainactivity.cs

       protected override void OnResume()
        {
            base.OnResume(); // Always call the superclass first.
        
        }
        protected override void OnPause()
        {
            base.OnPause(); // Always call the superclass first
        }
0 голосов
/ 24 мая 2018

Вы можете попробовать переопределить OnResume

protected async override void OnResume()
{
     base.OnResume();
     // Your Code. I would guess same code as you have in OnNewIntent
}
...