【问题标题】:How to detect User taps App Icon or Remote Notification Icon in Xamarin IOS如何在 Xamarin IOS 中检测用户点击应用程序图标或远程通知图标
【发布时间】:2018-11-23 22:25:49
【问题描述】:

我正在 xamarion IOS 中构建消息应用程序。我的问题如下

应用程序处于后台,并发出远程通知。现在用户可以通过单击应用程序图标和通知图标来打开应用程序。当收到通知和用户点击通知时,应用程序调用 didReceivedRemoteNotification。在这两种情况下,应用程序状态都是背景。 当用户点击通知时,我需要打开与通知相关的屏幕和 当用户在主屏幕上点击应用程序图标时,我需要打开默认屏幕。

无论是通过点击远程通知还是通过应用程序图标,我都无法识别该应用程序是否已打开。

编辑:应用程序仍在后台运行。

2) 应用程序在前台,用户锁定手机。 在这种情况下,App 的状态变为 Background,因为 WillEnterBackground 被调用。现在应用程序处于后台并接收到远程通知,并且应用程序状态触发的 DidReceiveRemoteNotification 事件为背景。用户点击通知并再次调用 DidReceiveRemoteNotification 并且状态为背景。所以我无法打开与通知相关的屏幕,因为我无法确定该事件是由于收到通知或由于收到通知而触发的。

我已经尝试了以下stackoverflow链接iOS push notification: how to detect if the user tapped on notification when the app is in background?中给出的解决方案

但仍然面临同样的问题。

【问题讨论】:

    标签: xamarin.ios


    【解决方案1】:

    当用户锁定屏幕时,应用程序将进入后台状态。所以当远程通知到达时,系统会在通知中心(屏幕顶部)提醒用户,DidReceiveRemoteNotification 不会被自动触发,因为应用程序处于前台状态。当应用程序处于后台状态时,只有一种状态可以触发DidReceiveRemoteNotification 事件(点击顶部的通知以打开应用程序)。这个背景状态包括你上面提到的情况:

    应用程序在前台,用户锁定手机。

    所以我认为并且我已经测试过:当用户锁定屏幕并收到通知时(在此操作之前,应用程序处于前台),DidReceiveRemoteNotification 将不会被自动调用。

    如果您在 iOS 10+ 上部署应用程序,另一种实现效果的方法是实现 IUNUserNotificationCenterDelegate 接口。在您的委托中实现以下两个事件:

    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        Console.WriteLine("Handling iOS 10 foreground notification");
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
    }
    
    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
    {
        completionHandler();
    }
    

    调用WillPresentNotification 将通知传递给在前台运行的应用程序。 DidReceiveNotificationResponse 只会在用户点击通知时触发,即使应用程序在前台。因此,您可以将您的代码从DidReceiveRemoteNotification 移动到DidReceiveNotificationResponse。此事件将确保代码仅在用户点击通知后运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多