201f610f48
This sample application demonstrates the new web client library for Microsoft Graph notifications, @microsoft/user-notifications-client version 0.1.7 (https://www.npmjs.com/package/@microsoft/user-notifications-client/v/0.1.7 ). The sample app lets you do the following: - Sign in using a Microsoft Azure Active Directory user account. If you'd like to use a personal, free-of-charge Microsoft account, follow the instructions in the included README file to modify the sample code. - Subscribe and unsubscribe for Microsoft Graph notifications. - Receive push notifications from the Graph notifications server. - Fetch Graph notifications (notification data is logged to the JavaScript console). - Update Graph notification state and delete Graph notifications. To do Azure AD user authentication, this sample app uses the Microsoft Authentication Library for JavaScript (MSAL.js) (https://github.com/AzureAD/microsoft-authentication-library-for-js ). Microsoft recommends you use MSAL when building your Graph notifications client app, but you can feel free to use any OAuth web authentication library. We hope you find this sample app useful! Code reviewer: Mariah Hart <mariahhart@users.noreply.github.com> Co-Authored-By: Dan Salajan <razvansalajan@users.noreply.github.com> Co-Authored-By: Michael Ratanapintha <metathinker@users.noreply.github.com> Closes #1 |
||
---|---|---|
.. | ||
.gitignore | ||
authreply.html | ||
index.html | ||
package-lock.json | ||
package.json | ||
readme.md | ||
register.ts | ||
service-worker.js | ||
tsconfig.json | ||
webpack.config.js |
readme.md
Sample App for running the Microsoft Graph notifications client library in your browser
Getting Started
Prerequisites
- Have node and npm installed. More details here
- Install http-server globally:
npm install http-server -g
Building the sample
npm install
(most of the time, only once is enough)npm run build
Running the sample
- Build the sample (if is not built already).
- Add the reply url
http://localhost:8080/authreply.html
(please see this for more details about the empty page for the reply url) to the redirect URIs list within the Authentication page of your Azure Active Directory client app registration. - Set the following values:
msalConfig.auth.clientId
in index.html: this is the client ID of your Azure AD client appcrossDeviceAppId
in index.html: this is the "cross-device domain name" that you have registered and verified in your Cross-Device App registration in Partner Center (https://partner.microsoft.com/dashboard )
- Open command prompt, powershell, or git for windows and start the http-server (by running
http-server
) within the folder of the sample app (i.e.: ~/repository-source-path/sample-app). - Open the page
http://localhost:8080/index.html
(check the port with the one given by http-server).- if the port is other than 8080 the 2nd step needs to be redone with the new given port.
- don't go to
http://localhost:8080/
withoutindex.html
, as that doesn't work in http-server.
- Sign in with an Azure AD account.
- see "Login with a personal Microsoft account (MSA)" below if you want to use that form of account.
- Click Subscribe, then click Allow on the Chrome browser prompt regarding notification permission.
- the JavaScript console can be opened to see the logs and results. Make sure that all the log levels are selected.
- Click the other buttons to exercise the various functions.
- as before, look in the JavaScript console for the logs and results.
- Post a new notification from Graph explorer and see a notification popup. Click the popup to be redirected to https://docs.microsoft.com/en-us/graph/notifications-concept-overview
How-to (mostly specific to Chrome and Firefox)
-
Login with a personal Microsoft account (MSA) instead of AAD:
- Change
msalConfig.auth.authority
tohttps://login.microsoftonline.com/consumers
- Change
requestObj.scopes
to["https://activity.windows.com/useractivity.readwrite.createdbyapp"]
- The clientId might need to be changed as well (in case 2 different apps are used) from the one used for an Azure AD account
- Reload the page
- Change
-
Unregister the service worker created by this app:
- Chrome:
- Option 1: Go to Dev Tools -> Application -> Service Workers -> (The one that you want) Unregister. More details in here
- Option 2: chrome://serviceworker-internals/. More details in here (first option)
- Firefox:
- Go to
about:serviceworkers
and unregister the one that you want
- Go to
- It's necessary to unregister the service worker when signing out and then signing in as a different user, or to troubleshoot when a push notification is not received.
- Chrome:
-
Check if Chrome has received a push notification: go to
chrome://gcm-internals
(Receive Message Log section). Also here the connection can be checked. More details in Payload Encryption Issue -
Force the
acquireTokenSilent
to call Azure AD instead of waiting 1 hour until the cached token expires; The following are 2 options for doing it:- one of key entry is deleted from the localStorage; more specially
{"authority":"https://login.microsoftonline.com/consumers/","clientId":"..","scopes":"https://activity.windows.com/UserActivity.ReadWrite.CreatedByApp","homeAccountIdentifier":"…"};
the one that has the scope set toactivity.*.com
(as there are 2 with the previous template) - forceRefresh flag
- one of key entry is deleted from the localStorage; more specially
Other tips
-
Getting duplicate push notifications? This is a known server-side issue in some cases. See the README for the client itself to learn more. Look for the text starting with:
userNotificationApiImpl.processPushNotificationAsync(notificationPayload) may call back to the Graph notification server to fetch notification data
-
To look at the logs of the sample app and the client library, open the developer tools -> console.
-
Wondering how the reload of index.html is avoided? Please see MSAL JS: 1.2.0 beta.0