Repository navigation
Expand file tree
/
Copy pathPushServiceBase.cs
More file actions
executable file
·118 lines (102 loc) · 4.1 KB
/
Copy pathPushServiceBase.cs
File metadata and controls
executable file
·118 lines (102 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
using System;
using Telegram.Api.Extensions;
using Telegram.Api.Helpers;
using Telegram.Api.Services;
using Telegram.Api.TL;
namespace TelegramClient.Services
{
public abstract class PushServiceBase : IPushService
{
protected abstract TLInt TokenType { get; }
protected readonly IMTProtoService Service;
protected PushServiceBase(IMTProtoService service)
{
Service = service;
}
public abstract string GetPushChannelUri();
private string _lastRegisteredUri;
private readonly object _lastRegisteredUriRoot = new object();
public virtual void RegisterDeviceAsync(Action callback)
{
Execute.BeginOnThreadPool(() =>
{
var channelUri = GetPushChannelUri();
if (string.IsNullOrEmpty(channelUri))
{
Telegram.Logs.Log.Write("PushServiceBase.RegisterDeviceAsync channelUri=null");
return;
}
var isAuthorized = SettingsHelper.GetValue<bool>(Constants.IsAuthorizedKey);
if (!isAuthorized)
{
Telegram.Logs.Log.Write("PushServiceBase.RegisterDeviceAsync isAuthorized=false");
return;
}
lock (_lastRegisteredUriRoot)
{
if (string.Equals(_lastRegisteredUri, channelUri))
{
Telegram.Logs.Log.Write("PushServiceBase.RegisterDeviceAsync lastRegisteredUri=channelUri " + channelUri);
return;
}
}
Service.RegisterDeviceAsync(
TokenType,
new TLString(channelUri),
result =>
{
lock (_lastRegisteredUriRoot)
{
_lastRegisteredUri = channelUri;
}
//Execute.ShowDebugMessage("account.registerDevice result " + result);
TLUtils.WriteLine("account.registerDevice result " + result);
callback.SafeInvoke();
},
error =>
{
//Execute.ShowDebugMessage("account.registerDevice error " + error);
TLUtils.WriteLine("account.registerDevice error " + error);
});
});
}
public virtual void UnregisterDeviceAsync(Action callback)
{
Execute.BeginOnThreadPool(() =>
{
var channelUri = GetPushChannelUri();
if (string.IsNullOrEmpty(channelUri))
{
Telegram.Logs.Log.Write("PushServiceBase.UnregisterDeviceAsync channelUri=null");
callback.SafeInvoke();
return;
}
Service.UnregisterDeviceAsync(
TokenType,
new TLString(channelUri),
result =>
{
lock (_lastRegisteredUriRoot)
{
_lastRegisteredUri = null;
}
TLUtils.WriteLine("account.unregisterDevice result " + result);
callback.SafeInvoke();
},
error =>
{
Execute.ShowDebugMessage("account.unregisterDevice error " + error);
TLUtils.WriteLine("account.unregisterDevice error " + error);
callback.SafeInvoke();
});
});
}
}
}