I'm building a notification system for a mobile app on Wordpress using One Signal.
Now Supposed to when I publish a new post sends a notification to all my Application devices.
The problem is I can't send post_meta with one signal notifications as it returns an undefined value first time when I hit the publish button.
But it works when I click update next time on the same post!! so why that delay/behavior?!
Same issue/behavior also with get_field
for ACF plugin.
The code of the filter that one signal using:
add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);
function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
// Change the notification's title, message, and URL
if($post->post_type == "mobile_notifications"){
$fields['headings'] = array("en" => wp_strip_all_tags($post->post_title));
$fields['contents'] = array("en" => wp_strip_all_tags($post->post_content));
$fields['data'] = array( 'type' => 'product', 'data' => get_post_meta( $post->ID, 'prefix_hash_product' ) );
// if ( !empty(get_field('target_product', $post->ID)) ) {
// $fields['data'] = array( 'type' => 'product', 'data' => get_field('target_product', $post->ID) );
// } elseif(!empty(get_field('target_category', $post->ID))) {
// $fields['data'] = array( 'type' => 'category', 'data' => get_field('target_category', $post->ID) );
// }
}
unset($fields['url']);
return $fields;
}