【发布时间】:2020-04-20 17:35:25
【问题描述】:
我有一个 xamarin.forms 应用程序,它实现了 firebase 推送通知。目前我正在玩 ios 部分。当应用程序处于前台、后台和被杀死时,我可以收到通知。问题是处理通知点击。我正在尝试从DidReceiveNotificationResponse 拨打MessagingCenter
方法以打开内容页面(表单中的我的通知详细信息页面)。我有两个疑问
我只处理来自
DidReceiveNotificationResponse的通知点击 方法。我可以处理这种方法的通知。目前我正在使用ios 12.4.6在 Iphone 上进行测试。处理较低操作系统版本的通知是否还有其他事情要做?还是对所有操作系统版本都足够了?当APP处于关闭或被杀状态时如何处理通知点击?目前应用在所有场景下都会导航到我的特定页面,但在被杀状态下,应用本身会在加载后关闭消息详情页面并自动导航到主屏幕。如何解决这个问题?
我的 DidReceiveNotificationResponse
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action
completionHandler)
{
completionHandler();
NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
var a = userInfo[new NSString("user_notification_id")] as NSString;
try
{
var myData = JsonConvert.DeserializeObject(userInfo[new NSString("user_notification_id")] as NSString);
if (myData != null)
{
Settings.NotificationID = myData.ToString();
Settings.NCNotificationStatus = null;
}
}
catch (Exception ex)
{
}
// Managing notification click here
MessagingCenter.Send<Object>(new Object(), "iosNcnotificationTapped");
}
我的 App.xaml.cs
public partial class App : Application
{
public string Isnotification;
public App(bool hasNotification = false)
{
InitializeComponent();
if (hasNotification)
{
var navPage = new NavigationPage(new LandingPage());
Application.Current.MainPage = navPage;
navPage.Navigation.PushModalAsync(new ChatPage("26"));
}
else
{
var splashPage = new CustomRender.TransitionNavigationPage(new SplashPage());
MainPage = splashPage;
}
MessagingCenter.Subscribe<Object>(this, "iosNcnotificationTapped", async (sender) => {
var navPage = new NavigationPage(new LandingPage());
Application.Current.MainPage = navPage;
await navPage.Navigation.PushModalAsync(new ChatPage("26"));
});
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
hasNotification 部分用于安卓部分。对于ios来说总是假的。如何解决这些问题?
【问题讨论】:
-
1.如果它适用于 iOS 12,我认为它也适用于较低版本。 2 试试this solution。这是一个 swift 版本,如果你需要,我可以将它翻译成 C#。
-
@JackHua-MSFT 感谢您的回复。我认为我的问题与 APP.xaml.cs 部分有关。当我们点击通知时,我的想法是加载应用程序部分也在调用。问题仅在应用被杀死状态
-
@JackHua-MSFT 您能帮我将您建议的解决方案转换为表单方式吗?
-
我添加了一个答案作为例子。
标签: xamarin xamarin.forms