我創建了一個地理位置應用程序,並且我想要自動啓動每次向我發送座標的服務的啓動。已關閉的應用程序應用程序觸發事件: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()'方法的完成回調中。 –