Dynamics Ninja Logo

Blog.

Enable in-app notifications easily

Cover Image for Enable in-app notifications easily
·
2 min read

Introduction

In-app notifications created a lot of hype on release day, but I still see that people struggle to set them up even with the step-by-step guide provided by Microsoft in the docs.

Since I hate repetitive tasks I created a simple Javascript snippet for one of my demos that will do everything for you with a simple copy & paste action in the dev tools console of your favorite browser. That way you can speed up enabling notifications in multiple apps with an almost one-click action.

Let's explain how to do it!

Solution

First of all, navigate the browser to the model-driven app that does not have in-app notifications enabled.

Next, you need to open the dev tool which can be done via simple keyboard shortcuts - F12 and Ctrl+Shift+I or you can select the option from the browser settings by clicking on the three dots -> More tools -> Developer Tools.

Navigate to the Console tab and paste the snippet below to it.

try {
    Xrm.Utility.showProgressIndicator("Enabling in-app notifications...");

    // Get app logical name
    var globalContext = Xrm.Utility.getGlobalContext();
    var appProperties = await globalContext.getCurrentAppProperties();
    var appName = appProperties.uniqueName;

    // Update app settings
    fetch(window.origin + "/api/data/v9.1/SaveSettingValue()", {
        method: "POST",
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            AppUniqueName: appName,
            SettingName: "AllowNotificationsEarlyAccess",
            Value: "true"
        })
    });

    // Publish All Customizations
    var publishAllXmlRequest = {
        getMetadata: function() {
            return {
                boundParameter: null,
                parameterTypes: {},
                operationType: 0,
                operationName: "PublishAllXml"
            };
        }
    };
    await Xrm.WebApi.online.execute(publishAllXmlRequest);

    Xrm.Utility.closeProgressIndicator();
    Xrm.Utility.alertDialog("In-app notifications are now enabled.");

    // Refresh the page
    setTimeout(function() {
        window.location.href = window.location.href;
    }, 3000);
} catch (error) {
    Xrm.Utility.closeProgressIndicator();
    Xrm.Utility.alertDialog(error);
}

You will see the loading screen that says Enabling in-app notifications... and after a few seconds if everything goes well you will have in-app notifications enabled for your app, otherwise, you will be presented with an error that you need to fix before running it again.

Now you should see the bell icon in the upper right corner of the screen if everything went well, if not, initiate Hard Reload (Ctrl + F5) just to be sure that you got the latest version of the app from the server.

It's time to start spamming your users with all kinds of notifications!

Conclusion

This is nothing revolutionary, but I'm sure it can save you some time while enabling the notifications on multiple apps in multiple enviromments. I'm also sure that Microsoft will soon provide a user-friendly way to achieve this, but until that, you can simply copy & paste the snippet and wait for work to be done for you.