【问题标题】:Xamarin IOS: How to handle notification tap when the app is not in the background?(killed state)Xamarin IOS:当应用程序不在后台时如何处理通知点击?(杀死状态)
【发布时间】:2019-11-23 19:19:12
【问题描述】:

我已经使用 Firebase Cloud Messaging 在我的 xamarin 表单项目(聊天应用程序)上实现了推送通知。所有州都收到通知,点击通知时我需要显示相应的消息列表页面。

当应用程序处于前台和后台模式时,通知点击工作。但是当应用程序不存在于后台(被杀死状态)时,点击不起作用。使用DidReceiveRemoteNotification我正在处理应用程序处于后台状态时的通知点击。使用WillPresentNotificationDidReceiveNotificationResponse 我正在显示通知并在前台模式下处理通知点击。下面添加代码:

 //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);
            }

完成发射

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            LoadApplication(new App());

            #region Push Notification            
            Firebase.Core.App.Configure();

            // Register your app for remote notifications.
            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                // iOS 10 or later
                var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
                UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
                    Console.WriteLine(granted);
                });

                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.Current.Delegate = this;

                // For iOS 10 data message (sent via FCM)
                //Messaging.SharedInstance.RemoteMessageDelegate = this;
            }
            else
            {
                // iOS 9 or before
                var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
                var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
            }

            UIApplication.SharedApplication.RegisterForRemoteNotifications();

            Messaging.SharedInstance.Delegate = this;
            Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
            #endregion

            return base.FinishedLaunching(app, options);
        }

我的通知正文: 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 中显示主页之前,消息列表显示进度(acr userdialogs)也出现在 UI 中。所以我认为FinishedLaunching中的LoadApplication(new App());在应用程序处于终止状态时在通知点击功能之后工作。那么如何在应用处于终止状态时停止执行正常的应用启动代码并显示消息列表页面?

更新

尝试如下。初始启动很好,但通过通知点击启动时崩溃。

  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);
            }
        }

【问题讨论】:

  • 希望对您有所帮助,如果还有什么问题请在此处ping。

标签: ios xamarin.forms push-notification


【解决方案1】:

您可以通过扩展 App 构造函数将数据传递给 Forms Project。

iOS 中的 AppDelegate

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);

【讨论】:

  • 这种情况下,如何处理应用的正常启动?
  • 在此处加载列表页面。
  • 是否可以使用MessagingCenter 而不是通过参数传递给 App.xaml.cs?因为我已经在前台和后台模式下使用过MessagingCenter
  • 如果myData为null表示应用正常启动,如果myData id不为null表示应用通过通知点击启动,对吧?
  • 在LoadApplication(new App());之后尝试发送MessagingCenter;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2016-02-10
  • 2020-04-24
  • 2020-10-23
  • 2022-01-22
相关资源
最近更新 更多