forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenarioNotificationReceived.cpp
159 lines (140 loc) · 6.13 KB
/
ScenarioNotificationReceived.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include <shellapi.h>
#include <shlwapi.h>
#include <sstream>
#include <string>
#include <windows.h>
#include "ScenarioNotificationReceived.h"
#include "App.h"
#include "CheckFailure.h"
#include "Util.h"
#include "resource.h"
using namespace Microsoft::WRL;
static constexpr WCHAR c_samplePath[] = L"ScenarioNotificationReceived.html";
ScenarioNotificationReceived::ScenarioNotificationReceived(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
m_webView2_24 = m_webView.try_query<ICoreWebView2_24>();
if (!m_webView2_24)
return;
//! [NotificationReceived]
// Register a handler for the NotificationReceived event.
CHECK_FAILURE(m_webView2_24->add_NotificationReceived(
Callback<ICoreWebView2NotificationReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NotificationReceivedEventArgs* args)
-> HRESULT
{
// Block notifications from specific URIs and set Handled to
// true so the the default notification UI will not be
// shown by WebView2 either.
CHECK_FAILURE(args->put_Handled(TRUE));
Microsoft::WRL::ComPtr<ICoreWebView2Deferral> deferral;
CHECK_FAILURE(args->GetDeferral(&deferral));
wil::unique_cotaskmem_string origin;
CHECK_FAILURE(args->get_SenderOrigin(&origin));
std::wstring originString = origin.get();
Microsoft::WRL::ComPtr<ICoreWebView2Notification> notification;
CHECK_FAILURE(args->get_Notification(¬ification));
notification->add_CloseRequested(
Callback<ICoreWebView2NotificationCloseRequestedEventHandler>(
[this, &sender](
ICoreWebView2Notification* notification, IUnknown* args) -> HRESULT
{
// Remove the notification from the list of active
// notifications.
RemoveNotification(notification);
return S_OK;
})
.Get(),
&m_notificationCloseRequestedToken);
m_appWindow->RunAsync(
[this,
notificationCom =
wil::make_com_ptr<ICoreWebView2Notification>(notification.Get()),
deferral, originString]()
{
ShowNotification(notificationCom.get(), originString);
deferral->Complete();
});
return S_OK;
})
.Get(),
&m_notificationReceivedToken));
//! [NotificationReceived]
}
bool ScenarioNotificationReceived::HandleWindowMessage(
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
{
if (message == WM_COMMAND)
{
switch (LOWORD(wParam))
{
case IDM_SCENARIO_NOTIFICATION:
NavigateToNotificationPage();
return true;
}
}
return false;
}
void ScenarioNotificationReceived::ShowNotification(
ICoreWebView2Notification* notification, std::wstring origin)
{
ICoreWebView2* webView = m_webView.get();
wil::unique_cotaskmem_string title;
CHECK_FAILURE(notification->get_Title(&title));
wil::unique_cotaskmem_string body;
CHECK_FAILURE(notification->get_Body(&body));
wil::unique_cotaskmem_string language;
CHECK_FAILURE(notification->get_Language(&language));
wil::unique_cotaskmem_string tag;
CHECK_FAILURE(notification->get_Tag(&tag));
wil::unique_cotaskmem_string iconUri;
CHECK_FAILURE(notification->get_IconUri(&iconUri));
wil::unique_cotaskmem_string badgeUri;
CHECK_FAILURE(notification->get_BadgeUri(&badgeUri));
wil::unique_cotaskmem_string imageUri;
CHECK_FAILURE(notification->get_BodyImageUri(&imageUri));
double timestamp;
CHECK_FAILURE(notification->get_Timestamp(×tamp));
BOOL requireInteraction;
CHECK_FAILURE(notification->get_RequiresInteraction(&requireInteraction));
BOOL silent;
CHECK_FAILURE(notification->get_IsSilent(&silent));
BOOL renotify;
CHECK_FAILURE(notification->get_ShouldRenotify(&renotify));
std::wstringstream message;
message << L"WebView2 has received an Notification: " << L"\n\t" << L"Sender origin: "
<< origin << L"\n\t" << L"Title: " << title.get() << L"\n\t" << L"Body: "
<< body.get() << L"\n\t" << L"Language: " << language.get() << L"\n\t" << L"Tag: "
<< tag.get() << L"\n\t" << L"IconUri: " << iconUri.get() << L"\n\t" << L"BadgeUri: "
<< badgeUri.get() << L"\n\t" << L"ImageUri: " << imageUri.get() << L"\n\t"
<< L"Timestamp: " << Util::UnixEpochToDateTime(timestamp) << L"\n\t"
<< L"RequireInteraction: " << ((!!requireInteraction) ? L"true" : L"false")
<< L"\n\t" << L"Silent: " << ((!!silent) ? L"true" : L"false") << L"\n\t"
<< L"Renotify: " << ((!!renotify) ? L"true" : L"false");
message << std::endl;
int response = MessageBox(nullptr, message.str().c_str(), title.get(), MB_OKCANCEL);
notification->ReportShown();
(response == IDOK) ? notification->ReportClicked() : notification->ReportClosed();
}
void ScenarioNotificationReceived::RemoveNotification(ICoreWebView2Notification* notification)
{
// Close custom notification.
// Unsubscribe from notification event.
CHECK_FAILURE(notification->remove_CloseRequested(m_notificationCloseRequestedToken));
}
void ScenarioNotificationReceived::NavigateToNotificationPage()
{
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
}
ScenarioNotificationReceived::~ScenarioNotificationReceived()
{
if (m_webView2_24)
{
CHECK_FAILURE(m_webView2_24->remove_NotificationReceived(m_notificationReceivedToken));
}
}