【问题标题】:Closed App application trigger the event :didReceiveRemoteNotification:关闭的 App 应用程序触发事件 :didReceiveRemoteNotification:
【发布时间】:2018-03-07 08:17:11
【问题描述】:

我正在创建一个地理定位应用程序,并且我想自动启动一个每次都会向我发送坐标的服务。

查看互联网,我读到从技术上讲这在 ios 上是不可能的,但是有一些方法可以解决这个问题。

我想采用静默通知。

我实现了该方法,当应用程序处于后台时一切正常,但是当我打开设备并发送静默通知时,事件:didReceiveRemoteNotification: 不会触发。 甚至当我从应用切换器关闭应用然后发送通知时也不会触发。

我想知道如果你能做到的话有什么问题。

ps:我正在使用:didReceiveRemoteNotification:并且没有应用程序:didReceiveRemoteNotification:fetchCompletionHandler:(这可能是问题吗?)

我在 AppDelegate 中的代码:

 public override async   void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> 
   completionHandler)
        {

             await Manager.SendPosition(completionHandler);
        }

在我的 FinishedLaunching 中:

if (Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0].ToString()) < 8)
            {
                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | 
                UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
            }
            else
            {
                UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
                var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet(new string[] { }));
                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }

在我的 info.plist 中:

   <key>UIBackgroundModes</key>
      <array>
        <string>location</string>
        <string>remote-notification</string>
      </array>

更新:

public async Task<bool> SendPosition(Action<UIBackgroundFetchResult> completionHandler = null)
        {
            StartLocationUpdates();
            var setting = ConnectDb()?.Table<Settings>().FirstOrDefault() ?? new Settings();
            _currentLong = 14.52538;
            _currentLat = 36.82842;
            if (_currentLong != 0 && _currentLat != 0)// && setting.TimeNotification != 0)
            {
                var objectSend = new
                {
                    unique_key = setting.UniqueKey, 
                    lat = _currentLat,
                    lng = _currentLong, 
                    idPlayerOneSignal = setting.IdPlayerOneSignal,
                    token = "" 
                };

                var client = new HttpClient { BaseAddress = new Uri("http://amywebservice.com/") };
                var data = JsonConvert.SerializeObject(objectSend);

                var content = new StringContent(data, Encoding.UTF8, "application/json");
                var response = await client.PostAsync("api/setPosition", content);
                if (response.IsSuccessStatusCode)
                {
                    var successResult = response.Content.ReadAsStringAsync().Result;
                    Debug.WriteLine("k", successResult);
                    if (completionHandler != null)
                    {
                        completionHandler(UIBackgroundFetchResult.NoData);
                    }
                    return true;
                }
                else
                {
                    var failureResulr = response.Content.ReadAsStringAsync().Result;
                    Debug.WriteLine("f", failureResulr);
                    if (completionHandler != null)
                    {
                        completionHandler(UIBackgroundFetchResult.NoData);
                    }
                    return false;
                }
            }
            return false;
        }

但不起作用,如果我发送静默通知,我只会运行第一次飞行,并且只有当应用程序处于后台时。

如果我打开手机并通知通知将不起作用。

【问题讨论】:

    标签: xamarin.ios silent-notification


    【解决方案1】:
    1. 根据DidReceiveRemoteNotification 上的 Xamarin 文档:

      [MonoTouch.Foundation.Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]

      Xamarin 中的DidReceiveRemoteNotification 与本机中的application:didReceiveRemoteNotification:fetchCompletionHandler: 相同。所以,方法是对的。

    2. 如果您已强制退出应用,除非您重新启动应用或重启设备,否则静默通知将不会启动您的应用。这是Related Apple documentation

      此外,如果您启用了远程通知后台模式,系统会启动您的应用(或将其从挂起状态唤醒)并在远程通知到达时将其置于后台状态。但是,如果用户强制退出,系统不会自动启动您的应用程序。在这种情况下,用户必须重新启动您的应用或重启设备,然后系统才会再次尝试自动启动您的应用。

    3. 我不太清楚你关于打开设备的描述(解锁你的设备而不打开应用程序?或其他。)在这里:

      但是当我打开设备并发送静音通知时

      但是根据application(_:didReceiveRemoteNotification:fetchCompletionHandler:)

      完成通知处理后,您必须在处理程序参数中调用该块,否则您的应用将被终止。您的应用程序有最多 30 秒的挂钟时间来处理通知并调用指定的完成处理程序块。 实际上,您应该在处理完通知后立即调用处理程序块。 系统会跟踪应用后台下载的经过时间、电源使用情况和数据成本。 在处理推送通知时使用大量电量的应用可能不会总是被提早唤醒以处理未来的通知。

      您必须在处理完通知后实现completionHandler

      DidReceiveRemoteNotification对应的地方添加completionHandler(),如if语句:

      • completionHandler(UIBackgroundFetchResult.NewData) 当你得到你想要的。
      • completionHandler(UIBackgroundFetchResult.Failed) 获取数据有任何问题时。
      • completionHandler(UIBackgroundFetchResult.NoData) 没有数据时。

      您的代码中似乎遗漏了它,这可能会影响静默通知。

    【讨论】:

    • 感谢您的澄清,但现在使用我发布的代码,如果我关闭手机并重新启动它并发送静默通知,我无法唤醒我的应用程序。可能是针对completionHandler的,completionHandler怎么操作?
    • 类似这个:public override async void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action&lt;UIBackgroundFetchResult&gt; completionHandler) { completionHandler(UIBackgroundFetchResult.NoData); await Manager.SendPosition(); }???
    • @BrunoMandarà,在发送完仓位后执行completionHandler,比如在SendPosition()方法的finished回调中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多