The short answer to this is "no, you cannot trigger a toast from Apex". This is because Apex is a server-side language and toasts must be launched from the front-end.
However, as @glls answer says, you can make a call to Apex, interpret its result, and then trigger a toast based on what results are. This is a very common pattern with errors, for example, where the Apex controller throws an exception and then that exception is shown to the user as a toast.
Here is a very brief example:
Apex:
public class myClass {
@AuraEnabled
public static String giveMeAMessage(String name) {
return 'Hello ' + name;
}
}
Lightning Aura Component:
callApex : function(component, event, helper) {
var action = component.get('c.giveMeAMessage');
action.setParams({
"name" : 'MYNAME'
});
action.setCallback(this, function(a){
var state = a.getState(); // get the response state
if(state == 'SUCCESS') {
helper.showToast(component, event, helper, a.getReturnValue());
} else {
helper.showToast(component, event, helper, 'ERROR');
}
});
$A.enqueueAction(action);
}
showToast : function(component, event, helper, message) {
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"message": "The Controller said: " + message
});
toastEvent.fire();
}
PS: You probably don't want to put the code above straight into production... there are better ways to make the call to Apex, but I wanted to show a quick example. Trailhead has several trails that will help with that part, including this one, which was also linked to in @glls's answer to this post.