Xamarin IOS:当应用程序不在后台时,如何处理通知点击?(已终止状态)
我已经使用Firebase云消息在我的xamarin表单项目(聊天应用程序)上实现了推送通知。在所有状态下都会收到通知,点击通知时,我需要显示相应的消息列表页面 当应用程序处于前台和后台模式时,通知点击正在工作。但是,当应用程序不在后台(已终止状态)时,点击不起作用。当应用程序处于后台状态时,我正在使用Xamarin IOS:当应用程序不在后台时,如何处理通知点击?(已终止状态),ios,xamarin.forms,push-notification,Ios,Xamarin.forms,Push Notification,我已经使用Firebase云消息在我的xamarin表单项目(聊天应用程序)上实现了推送通知。在所有状态下都会收到通知,点击通知时,我需要显示相应的消息列表页面 当应用程序处于前台和后台模式时,通知点击正在工作。但是,当应用程序不在后台(已终止状态)时,点击不起作用。当应用程序处于后台状态时,我正在使用didReceiveMemotentification处理通知点击。使用WillPresentNotification和direceivenotificationresponse我在前台模式下显示
didReceiveMemotentification
处理通知点击。使用WillPresentNotification
和direceivenotificationresponse
我在前台模式下显示通知并处理通知点击。代码添加如下:
//Notification tapping when the app is in background mode.
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
HandleMessage(userInfo);
// Print full message.
LogInformation(nameof(DidReceiveRemoteNotification), userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
var myData = JsonConvert.DeserializeObject<List<webContentList>>(userInfo[new NSString("webContentList")] as NSString);
Console.WriteLine($"myData received: {myData}");
if (UIApplication.SharedApplication.ApplicationState.Equals(UIApplicationState.Active))
{
//App is in foreground, no action
}
else
{
MessagingCenter.Send<object, List<webContentList>>(this, "messagedata", myData);
}
}
//Showing the notification when the app is in the foreground mode.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification,
Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
}
//Notification tapping when the app is in the foreground mode.
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action
completionHandler)
{
completionHandler();
NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
var myData = JsonConvert.DeserializeObject<List<webContentList>>(userInfo[new NSString("webContentList")] as NSString);
Console.WriteLine($"myData received: {myData}");
MessagingCenter.Send<object, List<webContentList>>(this, "messagedata", myData);
}
我的通知正文:webContentList
是我的模型数据
{
"to" : "dmtfiSvBBM0:APA91bFnHkamMSYgxPuiSfdvKnU8hD_mOqrWijnENNgXVSkSgo1ILH3-uKVCU7Ez2PXXOhtDoobIyKBf5UshVfTmvjSqHgXMRTsqguKCSTjIfGnXrVP-_cNFq2sisshZO-BcfkwKTl-I",
"collapse_key" : "type_a",
"notification" : {
"body" : "This is body",
"title": "Tech Team",
"priority":"high",
"content_available":true
},
"data" : {
"webContentList": [
{
"webContentDefinitionId": 818084,
"pageTitle": "CCD Grade 3-4",
"pageKwd": "CCD Grade 3-4",
"pageDesc": "CCD Grade 3-4",
"siteId": 45,
"pageCreatedTime": 1555145959428,
"pageUpdatedDate": 1555927274279,
"modifier": {
"userId": 12944,
"applicationId": 32,
"username": "robert.downey",
"email": "robert@master-mail.net",
"firstName": "Robert",
"lastName": "Downey"
},
"creator": {
"userId": 12944,
"applicationId": 32,
"username": "robert.downey",
"email": "robert@master-mail.net",
"firstName": "Robert",
"lastName": "Downey"
}
}
]
},
"ttl": 3600
}
问题
当应用程序处于终止状态时,只有主页正在加载,而不显示消息列表页面。但在UI中显示主页之前,UI中也会显示消息列表ShowProgress(acr userdialogs)。所以我认为LoadApplication(newapp())当应用程序处于终止状态时,FinishedLaunching
中的code>在通知点击功能后工作。那么,当应用程序处于终止状态时,如何停止正常应用程序启动代码的执行并显示消息列表页面呢
更新
像下面这样试过。初始发射状态良好,但通过通知点击发射时崩溃
LoadApplication(new App());
if (options != null)
{
var myData = JsonConvert.DeserializeObject<List<webContentList>>(options[new NSString("webContentList")] as NSString);
if(myData != null)
{
MessagingCenter.Send<object, List<webContentList>>(this, "messagedata", myData);
}
}
LoadApplication(新应用程序());
如果(选项!=null)
{
var myData=JsonConvert.DeserializeObject(选项[new NSString(“webContentList”)]作为NSString);
if(myData!=null)
{
发送(这是“messagedata”,myData);
}
}
您可以通过扩展应用程序构造函数将数据传递到Forms项目
iOS中的AppDelegate
public override bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
var myData=JsonConvert.DeserializeObject(选项[new NSString(“webContentList”)]作为NSString);
全局::Xamarin.Forms.Forms.Init();
LoadApplication(新应用程序(myData));
返回基地。完成发射(应用程序,选项);
}
表单中的应用程序
公共应用程序(列表)
{
初始化组件();
//处理逻辑
}
更新
LoadApplication(新应用程序());
发送(这是“messagedata”,myData);
您可以通过扩展应用程序构造函数将数据传递到Forms项目
iOS中的AppDelegate
public override bool FinishedLaunching(UIApplication应用程序、NSDictionary选项)
{
var myData=JsonConvert.DeserializeObject(选项[new NSString(“webContentList”)]作为NSString);
全局::Xamarin.Forms.Forms.Init();
LoadApplication(新应用程序(myData));
返回基地。完成发射(应用程序,选项);
}
表单中的应用程序
公共应用程序(列表)
{
初始化组件();
//处理逻辑
}
更新
LoadApplication(新应用程序());
发送(这是“messagedata”,myData);
在这种情况下,如何处理应用程序的正常启动?请在此处加载列表页面。是否可以使用MessagingCenter
而不是通过参数传递到app.xaml.cs?因为我已经在前台和后台模式下使用了MessagingCenter
。如果myData为null,则表示应用程序正在正常启动,如果myData id不为null,则表示应用程序正在通过通知点击启动,对吗?尝试发送MessagingCenter afterLoadApplication(新应用程序());在这种情况下,如何处理应用程序的正常启动?请在此处加载列表页面。是否可以使用MessagingCenter
而不是通过参数传递到app.xaml.cs?因为我已经在前台和后台模式下使用了MessagingCenter
。如果myData为null,则表示应用程序正在正常启动,如果myData id不为null,则表示应用程序正在通过通知点击启动,对吗?尝试发送MessagingCenter afterLoadApplication(新应用程序());希望它能帮助你,如果还有什么,请在这里ping。希望它能帮助你,如果还有什么,请在这里ping。
LoadApplication(new App());
if (options != null)
{
var myData = JsonConvert.DeserializeObject<List<webContentList>>(options[new NSString("webContentList")] as NSString);
if(myData != null)
{
MessagingCenter.Send<object, List<webContentList>>(this, "messagedata", myData);
}
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
var myData = JsonConvert.DeserializeObject<List<webContentList>>(options[new NSString("webContentList")] as NSString);
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(myData));
return base.FinishedLaunching(app, options);
}
public App(List<webContentList> list)
{
InitializeComponent();
// handle the logic
}
LoadApplication(new App());
MessagingCenter.Send<object, List<webContentList>>(this, "messagedata", myData);