私はジオロケーションアプリを作成しています。毎回座標を送信したサービスの起動を自動的に開始したいと考えていました。閉鎖されたアプリケーションアプリケーションがイベントをトリガーしました:didReceiveRemoteNotification:
私はインターネットを見て、それは技術的には可能ではありませんが、問題を克服する方法がいくつかあります。
私は静かな通知を採用したかったのです。
私はこのメソッドを実装しましたが、アプリがバックゴールードにあるときはすべて動作しますが、デバイスをオンにしてサイレント通知を送信すると、イベント:didReceiveRemoteNotification:がトリガーされません。 や、アプリのスイッチャーからアプリを終了して通知を送信しても、トリガーされません。
私はあなたがそれをすることができれば何か間違いがあるかどうかを知りたいと思っていました。
PS:私が使用している:didReceiveRemoteNotification:なしアプリケーション:didReceiveRemoteNotification:fetchCompletionHandler:(これは問題になる可能性が?)AppDelegateで
マイコード:私のFinishedLaunchingで
public 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;
}
ではなく、仕事私はサイレント通知を送信する場合、私は唯一の第1のフライを実行して、アプリがbackgorundにある場合にのみ。
私の電話をオンにして、通知通知が機能しない場合。
私は電話を切って再起動して通知を送信すると、私は自分のアプリを目覚めさせられません。それは完了のためかもしれませんハンドラ 私はどのように操作完了ハンドラに従うのですか?この類似 –
: 'パブリックオーバーライド非同期ボイドDidReceiveRemoteNotification(のUIApplicationアプリケーション、NSDictionaryのユーザー情報、アクション completionHandler) {completionHandler(UIBackgroundFetchResult.NoData)。 await Manager.SendPosition(); } ' ??? –
@BrunoMandarà、SendPosition()メソッドの終了コールバックのように、位置の送信が完了したら 'completionHandler'を実行します。 –