@@ -22,17 +22,10 @@ The following sections describe how to register for, handle, and show a Windows
22
22
``` C#
23
23
// App.xaml.cs
24
24
25
- protected override async void OnActivated (IActivatedEventArgs e )
26
- {
27
- await InitNotificationsAsync ();
28
-
29
- .. .
30
- }
31
-
32
25
protected override async void OnLaunched (LaunchActivatedEventArgs e )
33
26
{
34
27
await InitNotificationsAsync ();
35
-
28
+
36
29
.. .
37
30
}
38
31
@@ -48,7 +41,7 @@ private async Task InitNotificationsAsync()
48
41
49
42
if (result .ChannelUri != null )
50
43
{
51
- PNHChannelUri = result .ChannelUri . ToString ( );
44
+ PNHChannelUri = new Uri ( result .ChannelUri );
52
45
}
53
46
else
54
47
{
@@ -64,8 +57,8 @@ private async Task InitNotificationsAsync()
64
57
65
58
private void Channel_PushNotificationReceived (PushNotificationChannel sender , PushNotificationReceivedEventArgs args )
66
59
{
67
- switch (args .NotificationType )
68
- {
60
+ switch (args .NotificationType )
61
+ {
69
62
case PushNotificationType .Toast :
70
63
case PushNotificationType .Tile :
71
64
case PushNotificationType .TileFlyout :
@@ -75,11 +68,11 @@ private void Channel_PushNotificationReceived(PushNotificationChannel sender, Pu
75
68
var frame = (Frame )Window .Current .Content ;
76
69
if (frame .Content is MainPage )
77
70
{
78
- var mainPage = ( MainPage ) frame .Content ;
71
+ var mainPage = frame .Content as MainPage ;
79
72
await mainPage .HandlePushNotificationIncomingCallAsync (args .RawNotification .Content );
80
73
}
81
74
break ;
82
- }
75
+ }
83
76
}
84
77
```
85
78
@@ -93,9 +86,9 @@ Registration for push notifications needs to happen after successful initializat
93
86
94
87
this .callAgent = await this .callClient .CreateCallAgentAsync (tokenCredential , callAgentOptions );
95
88
96
- if ((Application .Current as App ).PNHChannelUri != String . Empty )
89
+ if ((Application .Current as App ).PNHChannelUri != null )
97
90
{
98
- await this .callAgent .RegisterForPushNotificationAsync ((Application .Current as App ).PNHChannelUri );
91
+ await this .callAgent .RegisterForPushNotificationAsync ((Application .Current as App ).PNHChannelUri . ToString () );
99
92
}
100
93
101
94
this .callAgent .CallsUpdated += OnCallsUpdatedAsync ;
@@ -110,11 +103,11 @@ To receive push notifications for incoming calls, call `handlePushNotification()
110
103
111
104
public async Task HandlePushNotificationIncomingCallAsync (string notificationContent )
112
105
{
113
- PushNotificationDetails pnDetails = PushNotificationDetails . Parse ( notificationContent );
114
- if ( this . callAgent != null )
115
- {
116
- await callAgent .HandlePushNotificationAsync (pnDetails );
117
- }
106
+ if ( this . callAgent != null )
107
+ {
108
+ PushNotificationDetails pnDetails = PushNotificationDetails . Parse ( notificationContent );
109
+ await callAgent .HandlePushNotificationAsync (pnDetails );
110
+ }
118
111
}
119
112
```
120
113
@@ -125,8 +118,8 @@ This triggers an incoming call event on CallAgent that shows the incoming call n
125
118
126
119
private async void OnIncomingCallAsync (object sender , IncomingCallReceivedEventArgs args )
127
120
{
128
- incomingCall = args .IncomingCall ;
129
- (Application .Current as App ).ShowIncomingCallNotification (incomingCall );
121
+ incomingCall = args .IncomingCall ;
122
+ (Application .Current as App ).ShowIncomingCallNotification (incomingCall );
130
123
}
131
124
```
132
125
@@ -135,12 +128,12 @@ private async void OnIncomingCallAsync(object sender, IncomingCallReceivedEventA
135
128
136
129
public void ShowIncomingCallNotification (IncomingCall incomingCall )
137
130
{
138
- string incomingCallType = incomingCall .IsVideoEnabled ? " Video" : " Audio" ;
139
- string caller = incomingCall .CallerDetails .DisplayName != " " ? incomingCall .CallerDetails .DisplayName : incomingCall .CallerDetails .Identifier .RawId ;
140
- new ToastContentBuilder ()
141
- .SetToastScenario (ToastScenario .IncomingCall )
142
- .AddText (caller + " is calling you." )
143
- .AddText (" New Incoming " + incomingCallType + " Call" )
131
+ string incomingCallType = incomingCall .IsVideoEnabled ? " Video" : " Audio" ;
132
+ string caller = incomingCall .CallerDetails .DisplayName != " " ? incomingCall .CallerDetails .DisplayName : incomingCall .CallerDetails .Identifier .RawId ;
133
+ new ToastContentBuilder ()
134
+ .SetToastScenario (ToastScenario .IncomingCall )
135
+ .AddText (caller + " is calling you." )
136
+ .AddText (" New Incoming " + incomingCallType + " Call" )
144
137
.AddButton (new ToastButton ()
145
138
.SetContent (" Decline" )
146
139
.AddArgument (" action" , " decline" ))
@@ -157,25 +150,23 @@ Add the code to handle the button press for the notification in the OnActivated
157
150
// App.xaml.cs
158
151
159
152
protected override async void OnActivated (IActivatedEventArgs e )
160
- {
161
- .. .
162
-
163
- // Handle notification activation
164
- if (e is ToastNotificationActivatedEventArgs toastActivationArgs )
165
- {
153
+ {
154
+ // Handle notification activation
155
+ if (e is ToastNotificationActivatedEventArgs toastActivationArgs )
156
+ {
166
157
ToastArguments args = ToastArguments .Parse (toastActivationArgs .Argument );
167
158
string action = args ? .Get (" action" );
168
-
159
+
169
160
if (! string .IsNullOrEmpty (action ))
170
161
{
171
- var frame = ( Frame ) Window .Current .Content ;
162
+ var frame = Window .Current .Content as Frame ;
172
163
if (frame .Content is MainPage )
173
164
{
174
- var mainPage = ( MainPage ) frame .Content ;
165
+ var mainPage = frame .Content as MainPage ;
175
166
await mainPage .AnswerIncomingCall (action );
176
167
}
177
168
}
178
- }
169
+ }
179
170
}
180
171
```
181
172
@@ -184,23 +175,23 @@ protected override async void OnActivated(IActivatedEventArgs e)
184
175
185
176
public async Task AnswerIncomingCall (string action )
186
177
{
187
- if (action == " accept" )
188
- {
178
+ if (action == " accept" )
179
+ {
189
180
var acceptCallOptions = new AcceptCallOptions ()
190
181
{
191
182
IncomingVideoOptions = new IncomingVideoOptions ()
192
183
{
193
184
StreamKind = VideoStreamKind .RemoteIncoming
194
185
}
195
186
};
196
-
187
+
197
188
call = await incomingCall ? .AcceptAsync (acceptCallOptions );
198
189
call .StateChanged += OnStateChangedAsync ;
199
190
call .RemoteParticipantsUpdated += OnRemoteParticipantsUpdatedAsync ;
200
- }
201
- else if (action == " decline" )
202
- {
191
+ }
192
+ else if (action == " decline" )
193
+ {
203
194
await incomingCall ? .RejectAsync ();
204
- }
195
+ }
205
196
}
206
197
```
0 commit comments