在推送通知上导航到特定页面单击

时间:2018-05-24 16:02:34

标签: xamarin.forms push-notification xamarin.android

我正在使用xamarin表格。在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());
        }

当app不在前台/后台时,我怎么能实现这个目标?

2 个答案:

答案 0 :(得分:0)

您可以尝试覆盖OnResume

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

答案 1 :(得分:0)

请尝试使用mainactivity.cs中的以下事件



       protected override void OnResume()
        {
            base.OnResume(); // Always call the superclass first.
        
        }
        protected override void OnPause()
        {
            base.OnPause(); // Always call the superclass first
        }