Get Started: Push Notification

Estimated reading time: 10 minutes

In this part, we will learn about sending push notifications to your application from mesibo.

Prerequisites

You MUST go through the following prerequisites before you read further.

  • Read the First App Guide.
  • Read one of the Android, iOS, JS section of this tutorial.

Push notification in mesibo

In the past, apps could stay online even when running in the background. However, it’s no longer the case. Both Android and iOS will suspend the app in a few seconds after the app moves to the background. Read more about Android and iOS background execution limits here and here.

Once your app is suspended by the operating system, it will not be able to receive new messages or calls untill it is activated again. Hence, when your app has a new message or a new incoming call, you need to wake up your app from the sleep state so that messages and calls can be delivered in real-time. This is where you need to send push notification. Push notification wakes up your app and moves it from the sleep state to the active state. Once the app moves to the active state, Mesibo will automatically connect and start receiving messages and calls. This entire process will be seamless for the user once you set up the push notifications.

There are two kinds of push notification:

  1. Background notification: This type of notifications are not visible (data type in Android and background or VoIP type in iOS).

  2. Alerts: These are visible alerts (notification type in Android and alert type in iOS).

By default, mesibo sends background notification that wakes your app in the background, and then the application can start mesibo to receive all the messages. Once your app receives messages, it can then use local notification to show the notification. This is a recommended way of receiving push for real-time application as it gives you the finer control on when and what to show in the notification. However, you can change this default behavior from the mesibo console by disabling the Background push checkbox. It will then send visible push alerts.

It is highly recommended that you refer to the mesibo messenger app source code to see how it is implemented there.

Setting up Push notification Credentials in mesibo Console

To enable mesibo to send push notification to your users, you need to configure push notification credentials in the mesibo console.

Follow the steps below to configure your push notification credentials

Configure FCM Credentials for Android Notifications

  1. Get the Firebase Sender ID and Firebase Server Key from Firebase Console of your app. Enter it in the mesibo console. Refer to the Firebase Documentation and FCM Messages for setting up your FCM Server.
  2. Go to your app settings in the mesibo console. Go to the section Push Notifications
  3. Enter the credentials that you got in the Android - Firebase Cloud Messaging (FCM) Configuration section.
  4. Under push, settings select Background Push (recommended) or set your required push notification type.

Configure APN/VoIP Credentials for iOS Notifications

  1. Get the universal Push/VoIP certificate in .p12 format from your Apple Developer Account. Refer to Obtain a Provider Certificate from Apple
  2. Go to your app settings in the mesibo console. Go to the section Push Notifications
  3. Choose the universal Push/VoIP certificate file that you downloaded earlier and upload it in the section Apple Push Notification (APN) Configuration.
  4. Under push, settings select Background Push (recommended) or set your required push notification type.

Sending push notification to your users

Once you setup the above information in the mesibo console, mesibo is almost ready to send push notification to your users. However, mesibo still requires a unique token per user so that it can send push notification to that particular user. You need to obtain that user-specific device token from using Android or iOS API and then send it to mesibo using setPushToken() API.

Obtaining push notification token in Android

  1. To understand how to get the user push token, refer Firebase Docs, Or this detailed tutorial on Android Push Notifications

  2. Use Mesibo setPushtoken() API to send push token to mesibo.

Refer to the Messenger App Source code for Android to understand how to do this.

Obtaining push notifications token in iOS

Unlike Android, iOS has two separate tokens, one for push and one for VoIP (if your app has call functionalities).

  1. Refer APN Docs or this detailed tutorial on iOS Push Notifications

  2. Use Mesibo setPushtoken() API to send push and VoIP tokens to mesibo.

Refer to the Messenger App Source code for iOS to understand how to do this.

Troubleshooting

if your app doesn’t get the push, check the following:

  • Ensure that you have configured push-notification credentials for your app correctly on the mesibo console.
  • Keep your phone on the cellular data and see if you are receiving push notifications
  • See if other apps on your phone receive it
  • mesibo will only send push when your app is suspended and disconnected. It may take a few seconds (or minutes) depending on the operating system and also the device configuration.

Another option is to send a push notification yourself and see if it helps. Use one of the scripts below to send push notification to your device. Once it is working, you can set up the mesibo webhook which will be invoked when the destination is offline. You can then send push notification from the webhook using your custom scripts.

Use the following PHP script to send push notifications to Android devices.

<?php

function android_push($to) {
	$apiKey = 'FCM API KEY';
	$url = 'https://fcm.googleapis.com/fcm/send';

	$priority = "high"; 
	$type = "data"; // or 'notification' . 

	$message = "none"; // your message
	$topic = ''; // not using, https://developers.google.com/cloud-messaging/topic-messaging
	$collapse_key = 'NEWMESSAGE'; 

	// there are two kinds of messages, data (default priority normal) and notification (default priority high). 
	$fields = array(
			'to' => $to,
			'priority' => $priority,
			'collapse_key' => $collapse_key,
			$type => array( "message" => $message ),
		       );

	$headers = array(
			'Authorization: key=' . $apiKey,
			'Content-Type: application/json'
			);

	// Open connection
	$ch = curl_init();

	// Set the URL, number of POST vars, POST data
	curl_setopt( $ch, CURLOPT_URL, $url);
	curl_setopt( $ch, CURLOPT_POST, true);
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields));

	// Execute post
	$result = curl_exec($ch);

	// Close connection
	curl_close($ch);

	$r = json_decode($result, 1);
	if(isset($r['failure']) && $r['failure'] > 0) {
		print_r($r);
		return false;
	}

	return true;
}

Use the following PHP script to send push notifications to iOS devices.

<?php
$token = "Client Token";

// APN certificate
$certificate = 'universal_cert.pem';

$message = '{"aps":{"action":"message","title":"your_title","body":"your_message_body"}}';
$apn_server = 'https://api.development.push.apple.com'; // or 'api.push.apple.com' if production
$bundle_id = 'com.mesibo.app';

$status = apn_push(true, "alert", $apn_server, $certificate, $bundle_id, $message, $token);
echo $status;

//type can be alert,voip, etc. Refer to APN documentation
function apn_push($production, $type, $apn_server, $certificate, $bundle_id, $message, $token) {

	$apn_server = 'https://api.development.push.apple.com';
	if($production) {
		$apn_server = 'https://api.push.apple.com';
	}


	$url = "{$apn_server}/3/device/{$token}";
	$cert = realpath($certificate);
	
	// headers
	$headers = array(
		"apns-topic: {$bundle_id}",
		"User-Agent: mesibo",
		"apns-push-type: {$type}",
		"apns-priority: 5",
	);

	// open connection 
	$ch = curl_init();

	curl_setopt_array($ch, array(
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
		CURLOPT_URL => $url,
		CURLOPT_PORT => 443,
		CURLOPT_HTTPHEADER => $headers,
		CURLOPT_POST => TRUE,
		CURLOPT_POSTFIELDS => $message,
		CURLOPT_RETURNTRANSFER => TRUE,
		CURLOPT_TIMEOUT => 30,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_SSLCERT => $cert,
		CURLOPT_HEADER => 1,
		CURLOPT_RETURNTRANSFER =>true
	));

	$result = curl_exec($ch);
	if ($result === FALSE) {
		$error = curl_error($ch);
		echo "curl faild: $error";
		curl_close($ch);
		return false;
	}

	// get response
	$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	if($status == "200")
		echo "OK";
	else
		echo "FAIL: $status";

	return false;

}

Voice & Video Calls >>

mesibo, android, ios