点击推送通知时,我需要导航到新页面。我不想重新启动该页面。这意味着,当我的应用程序已经在前台时,我只想导航到一个新页面(我需要导航栏中的后退图标(。但是,目前只能加载清除我现有导航堆栈并重新加载应用程序的应用程序。
下面是我在FirebaseMessagingService类中的代码。
public class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
var push = new Intent(this, typeof(PushActivity));
push.PutExtra("NotificationId", DateTime.Now.ToString());
push.AddFlags(ActivityFlags.ClearTop);
var fullScreenPendingIntent = PendingIntent.GetActivity(this, 0, push, PendingIntentFlags.CancelCurrent);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), @"d+").Value);
int id = new Random(seed).Next(000000000, 999999999);
notification.SetContentIntent(fullScreenPendingIntent)
.SetContentTitle(message.GetNotification().Title)
.SetContentText(message.GetNotification().Body);
manager.Notify(id, notification.Build());
}
}
下面是我的主活动代码。
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Firebase.FirebaseApp.InitializeApp(this);
string NotificationId = Intent.GetStringExtra("NotificationId");
if (NotificationId == null)
LoadApplication(new App(false));
else
LoadApplication(new App(true));
}
}
下面是我的应用程序.cs文件代码。
public partial class App : Application
{
private bool _isNotified;
public App(bool isNotified)
{
InitializeComponent();
this._isNotified = isNotified;
if (_isNotified)
MainPage = new NavigationPage(new ReceiveNotification());
else
MainPage = new NavigationPage(new MainPage());
this._isNotified = false;
}
public static void NavigateToReceiveNotificationPage()
{
App.Current.MainPage.Navigation.PushAsync(new ReceiveNotification());
}
}
注意:我已经通过在 iOS 中通过从渲染器调用 NavigateToReceiveNotificationPage 方法达到了这个要求。而在Android中,由于主要活动本身重新加载,因此不确定如何实现此要求。
您可以将MainActivityLaunchMode设置为SingleTop,然后在OnNewIntent方法中处理它,
[Activity(Label = "App18", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize,LaunchMode = LaunchMode.SingleTop )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
...
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
string NotificationId = intent.Extras.GetString("NotificationId");
//do something you want to do (according to your own needs,you also could use MessagingCenter to do what you want in forms page)
Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new Page());
}
...
}
消息传递中心
你可以参考类似的案例
更新:
将数据放入意图:
var push = new Intent(this, typeof(PushActivity));
var bundleForPush = new Bundle();
bundleForPush.PutString("NotificationId", DateTime.Now.ToString());
push.PutExtras(bundleForPush);
您可以使用Plugin.FirebasePushNotification,并且可以在App.xaml.cs中挂接它:
CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>{};