我创建了一个地理位置应用程序,并且我想要自动启动每次向我发送坐标的服务的启动。已关闭的应用程序应用程序触发事件:didReceiveRemoteNotification:
从互联网上看,我从技术上说它不可能在ios上,但有一些方法可以解决这个问题。
我想采用无声通知。
我实现了该方法,并且当应用程序在backgorund中时一切正常,但是当我打开设备并发送无声通知时,事件didReceiveRemoteNotification:不会触发。 甚至当我从应用程序切换器关闭应用程序,然后发送通知没有触发。
我想知道如果你能做到的话是否有什么问题。
PS:我使用的是:didReceiveRemoteNotification:没有应用:didReceiveRemoteNotification:fetchCompletionHandler:(可在此问题?)
我的代码中的AppDelegate:
我FinishedLaunchingpublic override async void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult>
completionHandler)
{
await Manager.SendPosition(completionHandler);
}
:
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>
UPDATE:
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;
}
但不工作,如果我发送一个无声的通知,我只运行了第一飞且仅当该应用程序是在backgorund。
如果我打开手机并通知通知不起作用。
感谢澄清,但现在的代码我张贴,如果我关掉手机,然后重新启动它,并发送一个无声的通知,我无法唤醒我的应用程序。它可能是为completionHandler 我该如何遵循操作completionHandler? –
类似于此: '公共覆盖异步空隙DidReceiveRemoteNotification(UIApplication的应用,的NSDictionary USERINFO,操作 completionHandler) { completionHandler(UIBackgroundFetchResult.NoData); await Manager.SendPosition(); }' ??? –
@BrunoMandarà,当你完成发送位置时执行'completionHandler',比如在'SendPosition()'方法的完成回调中。 –