【发布时间】:2017-06-28 14:30:03
【问题描述】:
我已经在我的应用程序上实现了推送通知,它们运行良好。 我遇到的问题是,当我在下拉菜单中单击它们时,它们会立即重新加载应用程序。为了解决这个问题,我让应用程序创建了一个新的活动实例。现在这会打开一个新页面,但是当从这个新页面返回时,它会遇到同样的问题并重新加载整个应用程序。
我将 Xamarin Forms 与 PCL 一起使用,因此它不是纯 Android。 有没有办法让菜单项上的点击事件在 PCL 中加载特定的页面视图?重新加载整个应用程序是没有用的。
这是创建电话通知的类:
ForegroundMessages.cs:
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class ForegroundMessages: FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
public override void OnMessageReceived(RemoteMessage message)
{
//Log.Debug(TAG, "From: " + message.From);
//Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
base.OnMessageReceived(message);
SendNotification(message.GetNotification().Body);
}
private void SendNotification(string body)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
// var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
Log.Debug(TAG, "Notification Message Body: " + body);
// sends notification to view model
MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase");
Intent resultintent = new Intent(this,typeof(SecondActivity));
TaskStackBuilder stackbuilder = TaskStackBuilder.Create(this);
stackbuilder.AddParentStack(Java.Lang.Class.FromType(typeof(SecondActivity)));
stackbuilder.AddNextIntent(resultintent);
PendingIntent resultPendingIntent = stackbuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
var notificationBuilder = new NotificationCompat.Builder(this)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle("Notification")
.SetContentText(body)
.SetAutoCancel(true)
.SetSound(defaultSoundUri)
.SetContentIntent(resultPendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
和第二个Activity类:
[Activity(Label = "Notifications")]
public class SecondActivity:Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
System.Diagnostics.Debug.WriteLine("this worked");
}
}
如上所述,这可以正常工作并接收消息,当您单击它们时,它会加载第二个活动,但是当离开第二个活动页面时,它只会重新加载整个应用程序。 如果我可以在 PCL 中加载视图页面(.xaml 或 .xaml.cs)而不是这个,那会更好。
有什么想法吗?
【问题讨论】:
-
所以我知道它为什么会重新加载整个应用程序。默认情况下,它会创建一个新的主要活动实例,因此会重新加载整个事情。所以我打算使用第二个活动作为中介来加载一个 xaml 页面。我会告诉你进展如何。
标签: c# xamarin xamarin.forms