The mobile and web application development platforms use the Firebase Cloud Messaging feature. Mainly it allows developers to send push notifications and messages to Android, iOS, and web platform devices. Moreover, Sending push notifications to mobile apps using WordPress is an easy task. So many WordPress plugins are available for connecting the wordpress to the Firebase and sending push notifications to the App. Here we discuss how to send the push notification to a mobile app using the WordPress custom function.
We have to send push notifications to the app in any of the actions. Here is an example code for sending notifications an on publish of a post.
function send_push_notification($deviceToken, $title, $message, $data =
array())
{
serverKey = 'YOUR_FIREBASE_SERVER_KEY'; //your Firebase server key
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization' => 'key=' . $serverKey,
'Content-Type' => 'application/json'
);
$notification = array(
'title' => $title,
'body' => $message,
'sound' => 'default',
'data' => $data
);
$fields = array(
'to' => $deviceToken,
'notification' => $notification
);
$args = array(
'headers' => $headers,
'body' => json_encode($fields),
);
$response = wp_remote_post($url, $args);
if (is_wp_error($response))
{
return false; // Error occurred during the request
}
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code === 200)
{
return true; // Notification sent successfully
}
else
{
return false; // Notification failed
}
}
The above will be added to the functions.php of the theme file. Replace YOUR_FIREBASE_SERVER_KEY
with your own Firebase server key. The wp_remote_post()
is the built-in function of WordPress that allows developers to interact with remote servers and services. ie, send an HTTP POST request to a specified URL. You have to customize the notification title and body.
function send_push_notification_on_publish($post_id, $post)
{
if ($post->post_type === 'custom_post' && $post->post_status ===
'publish')
{
$fcm_token = get_user_meta($user_id, 'fcm_token', true);
$user_email = get_user_email_by_id($user_id);
if ($fcm_token)
{
$notification = array(
'title' => $post->post_title,
'body' => $post->post_content,
)
);
send_push_notification($fcm_token, $notification);
}
}
}
add_action('save_post', 'send_push_notification_on_publish', 10, 2);
Add the above code in functions.php. Thes end_push_notification_on_publish the function is invoked in the publishing of a post. In conclusion, building a custom WordPress function to send push notifications to a mobile app via Firebase is an effective approach to interact and connect with app users.
Sreyas is the best option if you want to take your web-based business to the next level. We are the most reputable online and mobile application development firm. We also have extensive expertise in e-commerce development and design. Client satisfaction is extremely important to us, which is why we have completed over 300 projects worldwide.