0

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.

  • Does this answer your question? EVENT_AFTER_SAVE_ELEMENT triggered twice after saving an entry – Brad Bell Apr 27 at 4:35
  • 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? – nparabz Apr 27 at 5:22
  • It's working now!!! Thanks to both of you @andrew.welch and Brad Bell for the pointer! – nparabz Apr 29 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.