use*_*961 5 c# xamarin xamarin.forms
我已经在我的应用程序上实现了推送通知,它们工作正常。我的问题是,当我在下拉菜单中单击它们时,它们会立即重新加载该应用程序。为了解决这个问题,我让应用程序创建了一个新的活动实例。现在,这将打开一个新页面,但是当从该新页面单击返回时,它具有相同的问题并再次重新加载整个应用程序。
我将Xamarin Forms与PCL一起使用,因此它不是纯粹的Android。有没有一种方法可以使菜单项上的click事件加载到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());
}
}
Run Code Online (Sandbox Code Playgroud)
和第二个活动类:
[Activity(Label = "Notifications")]
public class SecondActivity:Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
System.Diagnostics.Debug.WriteLine("this worked");
}
}
Run Code Online (Sandbox Code Playgroud)
如前所述,这可以正常工作并收到消息,当您单击它们时,它会加载第二个活动,但是当离开第二个活动页面时,它将再次重新加载整个应用程序。如果我可以在PCL中加载视图页面(.xaml或.xaml.cs),则最好这样做。
有任何想法吗?
您可能会查看我的帖子,该帖子显示了我的操作方法,我还打开了一个“表单”页面。这篇文章很长,但基本上有几件事:
ActivityClearTop标志添加到MainActivity IntentTaskStackBuilder和而是使用PendingIntent.GetActivity获取PendingIntentMainActivity的设置LaunchMode为LaunchMode.SingleTop,OnNewIntent在您的中覆盖MainActivity,然后检查中在方法中设置intent.HasExtras的特殊string设置SendNotification以区别于Intent其他方法,专门解决了帖子中的应用重启问题Intent如果您以后仍然遇到问题,请告诉我。
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);
intent.PutExtra("SomeSpecialKey", "some special value");
Log.Debug(TAG, "Notification Message Body: " + body);
// sends notification to view model
MessagingCenter.Send((App)Xamarin.Forms.Application.Current, "Increase");
PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.OneShot);
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(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
Run Code Online (Sandbox Code Playgroud)
然后在MainActivity中:
[Activity(LaunchMode = LaunchMode.SingleTop, ....)]
public class MainActivity : FormsApplicationActivity {
protected override void OnNewIntent(Intent intent) {
if(intent.HasExtra("SomeSpecialKey")) {
System.Diagnostics.Debug.WriteLine("\nIn MainActivity.OnNewIntent() - Intent Extras: " + intent.GetStringExtra("SomeSpecialKey"); + "\n");
}
base.OnNewIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2538 次 |
| 最近记录: |