Reputation: 141
I have a wordpress website. I can manage to send push notifications to its android app using OneSignal. For this, I made a Must-Use plugin in the wordpress website following this documentation.
However, I'm wondering if it is possible to include the featured image from website inside the notification. Any help is needed.
Here is the code I used :
<?php
function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
$fields['isAndroid'] = true;
$fields['isIos'] = true;
$fields['isAnyWeb'] = false;
$fields['isChrome'] = false;
$fields['data'] = array(
"myappurl" => $fields['url']
);
/* Unset the URL to prevent opening the browser when the notification is clicked */
unset($fields['url']);
return $fields;
}
Upvotes: 0
Views: 582
Reputation: 1661
You can pass thumbnail id and url in the data like this:
function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
$ImageId = get_post_thumbnail_id($post->ID);
$ImageSrc = wp_get_attachment_image_src($ImageId);
$ImageUrl = $ImageSrc[0];
$fields['isAndroid'] = true;
$fields['isIos'] = true;
$fields['isAnyWeb'] = false;
$fields['isChrome'] = false;
$fields['data'] = array(
"myappurl" => $fields['url'],
"thumbnail_id" => $ImageId,
"thumbnail_url" => $ImageUrl,
);
/* Unset the URL to prevent opening the browser when the notification is clicked */
unset($fields['url']);
return $fields;
}
Upvotes: 2