我有一个应用程序,它允许通过xamarin sdk使用一个信号进行推送通知和即时通讯。
我的应用使用的是Arizona.forms 7.2和xamarin.forms 4.3.x
当我打开通知并开发了一些代码时,我想导航,但是导航存在问题。它没有按预期工作。尤其是应用无法在后台运行。
我可以获得推送通知,但是当我打开通知时没有导航,我想转到主页而不是产品详细信息页面。
不导航到我想要的页面。
这是我的app.cs代码
protected override async void OnInitialized()
{
InitializeComponent();
XF.Material.Forms.Material.Init(this);
if (Helpers.AppConstants.DebugMode)
{
// Handle Xamarin Form Logging events such as Binding Errors
Log.Listeners.Add(new Services.TraceLogListener());
}
InitAkavache();
InitGoogleAnalytics();
OneSignal.Current.StartInit(Helpers.AppConstants.OneSignalKey)
.Settings(new Dictionary<string, bool>()
{
{IOSSettings.kOSSettingsKeyAutoPrompt, false },
{IOSSettings.kOSSettingsKeyInAppLaunchURL, true },
})
.InFocusDisplaying(OSInFocusDisplayOption.Notification)
.UnsubscribeWhenNotificationsAreDisabled(true)
.HandleNotificationReceived((result) => HandleNotificationReceived(result))
.HandleNotificationOpened((result) => HandleNotificationOpened(result))
.EndInit();
if (!IsNotification)
{
await NavigationService.NavigateAsync(Screens.SplashScreenPage);
}
else
{
IsNotification = false;
}
}
private void HandleNotificationOpened(OSNotificationOpenedResult result)
{
IsNotification = true;
OSNotificationPayload payload = result.notification.payload;
Dictionary<string, object> additionalData = payload.additionalData;
if (additionalData.ContainsKey("type") && additionalData.ContainsKey("product"))
{
var productId = Convert.ToString(additionalData["product"]);
var id = Convert.ToInt64(productId);
NavigationService.NavigateAsync($"myapp:///{Screens.NavigationPage}/{Screens.MainTabbedPage}?{Prism.Navigation.KnownNavigationParameters.SelectedTab}={Screens.MainPage}", ("productId", id)).GetAwaiter().GetResult();
return;
}
else if (additionalData.ContainsKey("type") && additionalData.ContainsKey("category"))
{
var categoryId = Convert.ToString(additionalData["category"]);
var id = Convert.ToInt32(categoryId);
NavigationService.NavigateAsync($"myapp:///{Screens.NavigationPage}/{Screens.MainTabbedPage}?{Prism.Navigation.KnownNavigationParameters.SelectedTab}={Screens.MainPage}", ("categoryId", id)).GetAwaiter().GetResult();
return;
}
else if (additionalData.ContainsKey("type") && Convert.ToString(additionalData["type"]) == "open")
{
NavigationService.NavigateAsync($"myapp:///{Screens.NavigationPage}/{Screens.MainTabbedPage}?{KnownNavigationParameters.SelectedTab}={Screens.MainPage}").GetAwaiter().GetResult();
return;
}
else if (additionalData.ContainsKey("type") && additionalData.ContainsKey("coupon"))
{
IsNotification = false;
var coupon = Convert.ToString(additionalData["coupon"]);
if (!string.IsNullOrWhiteSpace(coupon))
{
Xamarin.Essentials.Clipboard.SetTextAsync(coupon).GetAwaiter().GetResult();
}
return;
}
}
private void HandleNotificationReceived(OSNotification notification)
{
OSNotificationPayload payload = notification.payload;
string message = payload.body;
Dictionary<string, object> additionalData = payload.additionalData;
if (additionalData != null && additionalData.ContainsKey("type"))
{
var item = new Data.Entity.Notification
{
Title = notification.payload.title,
SubTitle = notification.payload.subtitle,
Body = notification.payload.body,
AdditionalData = additionalData.Select(s => new Data.Entity.AdditionalData
{
Type = s.Key,
Value = Convert.ToString(s.Value)
}).ToList()
};
if (additionalData.ContainsKey("product"))
{
item.Type = Enums.NotificationType.Product;
}
else if (additionalData.ContainsKey("category"))
{
item.Type = Enums.NotificationType.Category;
}
else if (Convert.ToString(additionalData["type"]) == "open")
{
item.Type = Enums.NotificationType.Open;
}
else if (additionalData.ContainsKey("coupon"))
{
item.Type = Enums.NotificationType.Coupon;
}
else
{
item.Type = Enums.NotificationType.Unknown;
}
using (var notificationService = new NotificationLiteDbService())
{
notificationService.CreateItem(item);
}
}
}
编辑1:
如果应用程序在前台或后台运行,我可以获取通知和导航。 但是,如果应用程序关闭,我会收到通知,但无法导航。