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.