Skip to content

Commit e819084

Browse files
Update push-notifications-windows.md
1 parent dffcc17 commit e819084

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

articles/communication-services/how-tos/calling-sdk/includes/push-notifications/push-notifications-windows.md

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,10 @@ The following sections describe how to register for, handle, and show a Windows
2222
```C#
2323
// App.xaml.cs
2424
25-
protected override async void OnActivated(IActivatedEventArgs e)
26-
{
27-
await InitNotificationsAsync();
28-
29-
...
30-
}
31-
3225
protected override async void OnLaunched(LaunchActivatedEventArgs e)
3326
{
3427
await InitNotificationsAsync();
35-
28+
3629
...
3730
}
3831

@@ -48,7 +41,7 @@ private async Task InitNotificationsAsync()
4841

4942
if (result.ChannelUri != null)
5043
{
51-
PNHChannelUri = result.ChannelUri.ToString();
44+
PNHChannelUri = new Uri(result.ChannelUri);
5245
}
5346
else
5447
{
@@ -64,8 +57,8 @@ private async Task InitNotificationsAsync()
6457
6558
private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
6659
{
67-
switch (args.NotificationType)
68-
{
60+
switch (args.NotificationType)
61+
{
6962
case PushNotificationType.Toast:
7063
case PushNotificationType.Tile:
7164
case PushNotificationType.TileFlyout:
@@ -75,11 +68,11 @@ private void Channel_PushNotificationReceived(PushNotificationChannel sender, Pu
7568
var frame = (Frame)Window.Current.Content;
7669
if (frame.Content is MainPage)
7770
{
78-
var mainPage = (MainPage)frame.Content;
71+
var mainPage = frame.Content as MainPage;
7972
await mainPage.HandlePushNotificationIncomingCallAsync(args.RawNotification.Content);
8073
}
8174
break;
82-
}
75+
}
8376
}
8477
```
8578

@@ -93,9 +86,9 @@ Registration for push notifications needs to happen after successful initializat
9386
9487
this.callAgent = await this.callClient.CreateCallAgentAsync(tokenCredential, callAgentOptions);
9588

96-
if ((Application.Current as App).PNHChannelUri != String.Empty)
89+
if ((Application.Current as App).PNHChannelUri != null)
9790
{
98-
await this.callAgent.RegisterForPushNotificationAsync((Application.Current as App).PNHChannelUri);
91+
await this.callAgent.RegisterForPushNotificationAsync((Application.Current as App).PNHChannelUri.ToString());
9992
}
10093

10194
this.callAgent.CallsUpdated += OnCallsUpdatedAsync;
@@ -110,11 +103,11 @@ To receive push notifications for incoming calls, call `handlePushNotification()
110103
111104
public async Task HandlePushNotificationIncomingCallAsync(string notificationContent)
112105
{
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+
}
118111
}
119112
```
120113

@@ -125,8 +118,8 @@ This triggers an incoming call event on CallAgent that shows the incoming call n
125118
126119
private async void OnIncomingCallAsync(object sender, IncomingCallReceivedEventArgs args)
127120
{
128-
incomingCall = args.IncomingCall;
129-
(Application.Current as App).ShowIncomingCallNotification(incomingCall);
121+
incomingCall = args.IncomingCall;
122+
(Application.Current as App).ShowIncomingCallNotification(incomingCall);
130123
}
131124
```
132125

@@ -135,12 +128,12 @@ private async void OnIncomingCallAsync(object sender, IncomingCallReceivedEventA
135128
136129
public void ShowIncomingCallNotification(IncomingCall incomingCall)
137130
{
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")
144137
.AddButton(new ToastButton()
145138
.SetContent("Decline")
146139
.AddArgument("action", "decline"))
@@ -157,25 +150,23 @@ Add the code to handle the button press for the notification in the OnActivated
157150
// App.xaml.cs
158151
159152
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+
{
166157
ToastArguments args = ToastArguments.Parse(toastActivationArgs.Argument);
167158
string action = args?.Get("action");
168-
159+
169160
if (!string.IsNullOrEmpty(action))
170161
{
171-
var frame = (Frame)Window.Current.Content;
162+
var frame = Window.Current.Content as Frame;
172163
if (frame.Content is MainPage)
173164
{
174-
var mainPage = (MainPage)frame.Content;
165+
var mainPage = frame.Content as MainPage;
175166
await mainPage.AnswerIncomingCall(action);
176167
}
177168
}
178-
}
169+
}
179170
}
180171
```
181172

@@ -184,23 +175,23 @@ protected override async void OnActivated(IActivatedEventArgs e)
184175
185176
public async Task AnswerIncomingCall(string action)
186177
{
187-
if (action == "accept")
188-
{
178+
if (action == "accept")
179+
{
189180
var acceptCallOptions = new AcceptCallOptions()
190181
{
191182
IncomingVideoOptions = new IncomingVideoOptions()
192183
{
193184
StreamKind = VideoStreamKind.RemoteIncoming
194185
}
195186
};
196-
187+
197188
call = await incomingCall?.AcceptAsync(acceptCallOptions);
198189
call.StateChanged += OnStateChangedAsync;
199190
call.RemoteParticipantsUpdated += OnRemoteParticipantsUpdatedAsync;
200-
}
201-
else if (action == "decline")
202-
{
191+
}
192+
else if (action == "decline")
193+
{
203194
await incomingCall?.RejectAsync();
204-
}
195+
}
205196
}
206197
```

0 commit comments

Comments
 (0)