【发布时间】:2017-03-27 18:15:58
【问题描述】:
我目前正在开发适用于 iOS 和 Android 的 Xamarin 应用程序,但我要解释的问题仅涉及 Android 应用程序(iOS 应用程序中尚未实现)。
实际上,当我收到给定的推送通知时,我需要在我的应用程序中打开一个特定页面。如果在收到推送通知时应用程序处于打开状态,它会很好地工作,但如果我的应用程序关闭或在后台运行,应用程序会崩溃。
好吧,当我收到通知时,我最终会使用名为“OnShouldOpenCommand”的方法:
private void OnShouldOpenCommand(string commandId)
{
NotifyNewCommand(AppResources.AppName, AppResources.CommandNotificationText, commandId);
Device.BeginInvokeOnMainThread(() =>
{
try
{
App.MasterDetailPage.Detail = new NavigationPage(new CommandAcceptancePage(commandId))
{
BarBackgroundColor = Color.FromHex("1e1d1d")
};
App.MasterDetailPage.NavigationStack.Push(((NavigationPage)(App.MasterDetailPage.Detail)).CurrentPage);
}
catch(Exception e)
{
Log.Debug("PushAsync", "Unable to push CommandAcceptancePage : "+ex.Message);
}
});
}
private void NotifyNewCommand(string Title,string Description, string commandId)
{
var intent = new Intent(this, typeof(MainActivity));
if (!String.IsNullOrEmpty(commandId))
{
intent.PutExtra("CommandId", commandId);
}
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
var notificationBuilder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle("Kluox")
.SetContentText(Description)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
Notification notification = notificationBuilder.Build();
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notification);
}
还有代码
App.MasterDetailPage.Detail = new NavigationPage(new CommandAcceptancePage(commandId))
{
BarBackgroundColor = Color.FromHex("1e1d1d")
};
正在生成类型异常:
Java.Lang.IllegalStateException: 之后无法执行此操作 onSaveInstanceState
好吧,如果我的应用程序没有在前台运行,我想我无法访问“应用程序”并重定向到另一个页面。好吧,这是我收到推送通知的时候,而不是我点击它的时候。但是,我不打算通过这样做来重新打开我的应用程序。
因为在那之后,当我点击名为 Kluox 的推送通知(这应该重新打开我的应用程序)时,应用程序崩溃了,我真的不知道为什么,我不知道在哪里放置断点能够调试,因为 Visual Studio 只是告诉我“发生了未处理的异常。”。
有人可以帮我吗?如果你需要任何代码,你可以问我,我会编辑我的消息并给你任何你需要的信息!
编辑 1:这是我的 OnCreate 方法的代码:
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
var info = Intent.Extras?.GetString("CommandId", "");
global::Xamarin.Forms.Forms.Init(this, bundle);
if (String.IsNullOrEmpty(info))
{
LoadApplication(new App());
}
else
{
LoadApplication(new App(info));
}
if (instance == null)
{
instance = this;
RegisterWithGCM();
}
else
{
instance = this;
}
}
【问题讨论】:
-
我也想知道答案!请有人帮忙!
-
以防万一,我发现有人有同样的问题,但没有回应:forums.xamarin.com/discussion/comment/261562#Comment_261562
标签: xamarin push-notification xamarin.forms