如何在FCM通知点击Xamarin表单后打开内容页?
我已经在xamarin forms android上实现了FCM通知。所以我想,当用户单击通知时,它将打开xamarin forms内容页。 以下是我接收FCM通知的代码: MyFireMessagingService类内部-如何在FCM通知点击Xamarin表单后打开内容页?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我已经在xamarin forms android上实现了FCM通知。所以我想,当用户单击通知时,它将打开xamarin forms内容页。 以下是我接收FCM通知的代码: MyFireMessagingService类内部- public override void OnMessageReceived(RemoteMessage message) { base.OnMessageReceived(message); SendNot
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
}
public void SendNotificatios(string body, string Header)
{
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.AppLauncher);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.AppLauncher));
builder.SetContentTitle(Header);
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
}
那么,如何在单击通知后打开内容页呢?作为我替换的第一件事
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
}
与
然后创建了一个新方法CreateNotification,如下所示
private void CreateNotification(Object e)
{
try
{
string title = "";
string body = "";
var intent = new Intent(this, typeof(MainActivity));
var i = e as Intent;
var bundle = i.Extras;
title = bundle.GetString("gcm.notification.title");
body = bundle.GetString("gcm.notification.body");
intent.PutExtra("title", title);
intent.PutExtra("body", body);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop );
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.AppLauncher);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.AppLauncher));
builder.SetContentTitle(Header);
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
现在,我们必须更新MainActivity.cs,通过覆盖OnNewIntent方法来处理推送通知单击,如下所示
protected async override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
var title = intent.GetStringExtra("title");
//This method will get called while the app is launching from the app icon or from the notification
if (title != null)
{
//Means new Intent from push notification
//Code to open the page
}
}
确保您在活动中保留了LaunchMode=LaunchMode.SingleTask好问题,使用Xamarin Android,您只需在您的意图中添加一个额外的内容,即可从您用intent.GetStringExtra指示的活动中处理它,但这里只有一个活动。。。我认为有解决方法,比如Montemagno解决了权限问题(),我担心我不够专业。PermissionsPlugin不是解决这个问题的解决方案。我不是说它解决了这个问题,只是方法可能类似。因为理论上,对于权限,您必须在每个活动中处理它们,而在Xamarin表单中只有一个。
protected async override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
var title = intent.GetStringExtra("title");
//This method will get called while the app is launching from the app icon or from the notification
if (title != null)
{
//Means new Intent from push notification
//Code to open the page
}
}