[SalesForce] Error Implementing force:showToast – Cannot read property ‘setParams’ of undefined

Component

<aura:component implements="force:lightningQuickAction">
    <ui:inputNumber aura:id="num1"/> +
    <ui:inputNumber aura:id="num2"/>
    <br/>
    <ui:button label="Add" press="{!c.showToast}"/>
</aura:component>

Controller

  ({
        showToast : function(component, event, helper) {
        var n1 = component.find("num1").get("v.value");
        var n2 = component.find("num2").get("v.value");

        // Display the total in a "toast" status message

        var resultsToast = $A.get("e.force:showToast");
        resultsToast.setParams({
            "title": "Quick Add: " + n1 + " + " + n2,
            "message": "The total is: " + (n1 + n2) + "."
        });
        resultsToast.fire();

        // Close the action panel

        var dismissActionPanel = $A.get("e.force:closeQuickAction");
        dismissActionPanel.fire();
        }
    })

ERROR MESSAGE

This page has an error. You might just need to refresh it. Action
failed: c$TestCheck1$controller$showToast [Cannot read property
'setParams' of undefined] Failing descriptor:
{c$TestCheck1$controller$showToast}

Best Answer

If you're using Preview from Dev Console this won't work (or, as Praveen pointed out from Visualforce), you'd have to test it in lightning experience.

The documentation states:

This event is handled by the one.app container. It’s supported in Lightning Experience and Salesforce1 only.

I can only assume that Aura preview is not Lightning Experience (would think its to do with the full app framework not being loaded in preview).