Body: <p>Newbie to Craft CMS. I'm trying to send a push notification to an Android device from a Craft CMS module when an Entry of a particular type (NewsItem) is created / published. Here is the relevant code (which I wrote with the help of StackExchange's very helpful developers):</p>
  
  <pre><code>        // Custom initialization code goes here...
          Craft::$app-&gt;elements-&gt;on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {
  
              // Make sure it's an entry.
              if ($event-&gt;element instanceof craft\elements\Entry) {
                  $entry = $event-&gt;element;
                  $entryType = $entry-&gt;getType()-&gt;handle;
  
                  // Check if it's in the section you care about
                  if ($entry-&gt;getSection()-&gt;handle == 'newsitems' &amp;&amp; (($entryType == 'newsitemPhoto') || ($entryType == 'newsitemVideo'))) {
                      // Do your custom logic
  
                          if($entryType == 'newsitemPhoto')
                              $image_name = $entry-&gt;photo-&gt;one()-&gt;getUrl();
                          else
                              $image_name = 'http://img.youtube.com/vi/' . $entry-&gt;youtubeVideoCode . '/0.jpg';
  
                          $data = array
                          (
                              'title'  =&gt; 'News from MY APP',
                              'content' =&gt; $entry-&gt;title,
                              'imageUrl' =&gt; $image_name,
                              'articleId' =&gt; $entry-&gt;id,
                          );
  
                         // Code for sending data via push notifications to Android phone
  
  
  </code></pre>
  
  <p>When I create a <em>single</em> entry, I get 4 notifications on my phone instead of one. Here are the debug messages from Android Studio:</p>
  
  <pre><code>D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -&gt; News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -&gt; null
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -&gt; http://img.youtube.com/vi//0.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -&gt; 147
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -&gt; News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -&gt; Test NewsItem
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -&gt; http://img.youtube.com/vi//0.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -&gt; 147
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -&gt; News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -&gt; Test NewsItem
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -&gt; http://path.to.image/489.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -&gt; 147
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -&gt; News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -&gt; Test NewsItem
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -&gt; http://path.to.image/489.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -&gt; 148
  
  </code></pre>
  
  <p>As can be seen, for one entry I created, I get 4 different messages with different data in the fields (even the id is different in the last message). I'm assuming that the event handler in my craft module is getting called 4 times. Is this correct? I would like to debug this in Craft itself, but not sure how to write messages to log file from a module. I tried Craft::info(), but the messages are not showing up in my Craft log files.</p>
  
  <p>Any help would be appreciated. Thanks in advance.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-26T22:17:58.043Z
Comments.Text: See: https://craftcms.stackexchange.com/questions/31235/event-after-save-element-triggered-twice-after-saving-an-entry
Comments.UserDisplayName: andrew.welch
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-27T04:35:59.660Z
Comments.Text: Does this answer your question? [EVENT\_AFTER\_SAVE\_ELEMENT triggered twice after saving an entry](https://craftcms.stackexchange.com/questions/31235/event-after-save-element-triggered-twice-after-saving-an-entry)
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-27T05:22:16.823Z
Comments.Text: Thanks @andrew.welch and Brad Bell for your links. I thought this would solve the problem, but when I add the condition: !ElementHelper::isDraftOrRevision($entry) or !$entry->getIsDraft() to my code, the notifications stop coming to my Android phone. I still can't debug. I tried adding Andrew's code of throwing an exception and logging it to my log file, but nothing shows up in my web.log file. Is there any setting I'm missing?
Comments.UserDisplayName: nparabz
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-29T02:24:30.927Z
Comments.Text: It's working now!!! Thanks to both of you @andrew.welch and Brad Bell for the pointer!
Comments.UserDisplayName: nparabz
Tags: <craft3>
Title: Elements::EVENT_AFTER_SAVE_ELEMENT - Event handler (in module) being called multiple times on creating a single element
ThreadText: # Elements::EVENT_AFTER_SAVE_ELEMENT - Event handler (in module) being called multiple times on creating a single element
  
  (Asked by: username_0 on 2020-04-26)
  
  Newbie to Craft CMS. I'm trying to send a push notification to an Android device from a Craft CMS module when an Entry of a particular type (NewsItem) is created / published. Here is the relevant code (which I wrote with the help of StackExchange's very helpful developers):
  
  ```
          // Custom initialization code goes here...
          Craft::$app->elements->on(Elements::EVENT_AFTER_SAVE_ELEMENT, function(ElementEvent $event) {
  
              // Make sure it's an entry.
              if ($event->element instanceof craft\elements\Entry) {
                  $entry = $event->element;
                  $entryType = $entry->getType()->handle;
  
                  // Check if it's in the section you care about
                  if ($entry->getSection()->handle == 'newsitems' && (($entryType == 'newsitemPhoto') || ($entryType == 'newsitemVideo'))) {
                      // Do your custom logic
  
                          if($entryType == 'newsitemPhoto')
                              $image_name = $entry->photo->one()->getUrl();
                          else
                              $image_name = 'http://img.youtube.com/vi/' . $entry->youtubeVideoCode . '/0.jpg';
  
                          $data = array
                          (
                              'title'  => 'News from MY APP',
                              'content' => $entry->title,
                              'imageUrl' => $image_name,
                              'articleId' => $entry->id,
                          );
  
                         // Code for sending data via push notifications to Android phone
  
  ```
  
  When I create a *single* entry, I get 4 notifications on my phone instead of one. Here are the debug messages from Android Studio:
  
  ```
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -> News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -> null
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -> http://img.youtube.com/vi//0.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -> 147
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -> News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -> Test NewsItem
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -> http://img.youtube.com/vi//0.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -> 147
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -> News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -> Test NewsItem
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -> http://path.to.image/489.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -> 147
  D/MyFirebaseMessagingService: onMessageReceived...
  D/MyFirebaseMessagingService: sendNotification... Title -> News from MY APP
  D/MyFirebaseMessagingService: sendNotification... Content -> Test NewsItem
  D/MyFirebaseMessagingService: sendNotification... ImageUrl -> http://path.to.image/489.jpg
  D/MyFirebaseMessagingService: sendNotification... Article Id -> 148
  
  ```
  
  As can be seen, for one entry I created, I get 4 different messages with different data in the fields (even the id is different in the last message). I'm assuming that the event handler in my craft module is getting called 4 times. Is this correct? I would like to debug this in Craft itself, but not sure how to write messages to log file from a module. I tried Craft::info(), but the messages are not showing up in my Craft log files.
  
  Any help would be appreciated. Thanks in advance.
  
  **Tags:** craft3
  
  > **Comments:**
  > **username_1**: See: https://craftcms.stackexchange.com/questions/31235/event-after-save-element-triggered-twice-after-saving-an-entry  
  > **username_2**: Does this answer your question? [EVENT\_AFTER\_SAVE\_ELEMENT triggered twice after saving an entry](https://craftcms.stackexchange.com/questions/31235/event-after-save-element-triggered-twice-after-saving-an-entry)  
  > **username_0**: Thanks @username_1 and username_2 username_2 for your links. I thought this would solve the problem, but when I add the condition: !ElementHelper::isDraftOrRevision($entry) or !$entry->getIsDraft() to my code, the notifications stop coming to my Android phone. I still can't debug. I tried adding Andrew's code of throwing an exception and logging it to my log file, but nothing shows up in my web.log file. Is there any setting I'm missing?  
  > **username_0**: It's working now!!! Thanks to both of you @username_1 and username_2 username_2 for the pointer!  
  
  ---
  
  ## 0 answers
  
  