Mesibo Real-time Push Notification

Estimated reading time: 14 minutes

Mesibo allows you to send push notifications to all your users without using any external services. Push notification is only required when there is a new message or calls for one of your users, and the user device is in the sleep state. Mesibo automates the entire push notification handling for you without any intervention from your end. All you need to do is configure your FCM/APN push credentials in the console. Then, mesibo will automatically send push notifications to your users on behalf of you.

Why Push Notification Needed - A Quick Overview

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 the operating system suspends your app, it will not be able to run and hence receive new messages or calls until 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 to deliver messages and calls in real-time. This is where you need to send push notifications. 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 notification is 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 a 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 notifications 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. Go to your App settings in the mesibo console. Go to the section Push Notifications and turn ON the switch to enable push notifications. Now, enter the necessary details in Android - Firebase Cloud Messaging (FCM) Configuration
  2. 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.
  3. Enter the credentials that you got- Firebase Sender ID and Firebase Server Key in - 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. Go to your App settings in the mesibo console. Go to the section Push Notifications and turn ON the switch to enable push notifications. Now, enter the necessary details in Apple Push Notification (APN) Configuration
  2. Get the universal Push/VoIP certificate in .p12 format from your Apple Developer Account. Refer to Obtain a Provider Certificate from Apple
  3. Choose the universal Push/VoIP certificate file that you downloaded earlier and upload it in the Apple Push Notification (APN) Configuration section.
  4. Under push, settings select Background Push (recommended) or set your required push notification type.

Use the Production Certificate, not the development certificate. Mesibo console will give you a warning but will accept the certificate. However, your device is likely not to receive push notifications.

Push Settings

You can configure additional push notification settings in the mesibo console.

  • Background Push, By default, mesibo will send ‘Background Push’, which is recommended. However, you can change it to alert by disabling the ‘Background Push’ checkbox.

  • Sound, Play a notification sound to alert the user. You can enable this only if you have disabled background push(silent push)

  • Collapse Notifications, Check this box to group multiple notifications and make them collapsible.

  • Send Sandbox Push to APN Production server, If you want all your push requests to go to the production APN server even while using a development environment, enable this option. For more information, refer to the Troubleshooting section.

  • Autoroute iOS Push through APN or FCM, If you have configured FCM credentials and would like to route iOS push through FCM, check this option.

  • Print Push Logs, If you are using on-premise, check this to enable Debug Logs. You can then see logs in the on-premise server logs for every push request send to FCM or APN and the result. If you are using mesibo cloud, see Debugging Push Notifications using Webhooks

Sending push notification to your users

Once you setup the above information in the mesibo console, mesibo is almost ready to send push notifications to your users. However, mesibo still requires a unique token per user so that it can send push notifications 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.

Sending push notification token in Android

  1. To understand how to get the user push token, refer to 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, which uses setPushtoken API

Sending push notification token in iOS

Unlike Android, iOS has two separate tokens, one for push and one for VoIP (PushKit). You only need to use push token.

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

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

Refer to the Messenger App Source code for iOS, which uses setPushtoken API

Troubleshooting

General 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.

Additional Troubleshooting Steps for iOS

  • Ensure that you are not using PushKit Tokens. iOS will terminate your app if you misuse PushKit tokens. Refer to Apple documentation and also WWDC talk Advances in App Background Execution.
  • Use the Production Certificate, not the development certificate. Mesibo console will give you a warning but will accept the certificate. However, your device is likely not to receive push notifications.
  • Ensure that the bundle ID on certficate matches your app bundle ID.
  • Don’t mess with the APS entitlements setting unless you know what you are doing. By default, it is set to development for the development and set to Production by Xcode when you upload your app to the App Store for release or test flight. This setting impacts the selected APN endpoint for sending push notifications. Hence, if you change this to say ‘Production’, you may need to send all your sandbox pushes to the production APN server

Troubleshooting using Custom Scripts

Another troubleshooting option is to send a push notification yourself and see if it helps. Use one of the scripts below to send push notifications 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 notifications from the webhook using your custom scripts. You can also use webhooks to inspect the push request sent by mesibo and the response received from FCM/APN. See Debugging Push Notifications using Webhooks.

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;

}

Troubleshooting using Webhooks

If your app cannot receive push notifications, even after troubleshooting with the steps above, you can also debug the issue with push notifications using webhooks.

You can inspect the push request and response, which can be made available to you through webhooks. To do this, check Notify failed push notifications while setting up Webhook, in the console. When a push notification fails, you will receive a webhook request( a POST request) from mesibo, and the payload will contain information about the push event. In the webhook payload, you can inspect the request and the response so that you can understand the reason for failure or why the push service(FCM/APN) rejected the push. For example, if you did not configure the right credentials, etc.

Refer mesibo Real-time webhooks to know about setting up a webhook and using the push event type to debug failed push notifications.

In the push event, the response.code field will contain the error code which indicates the reason for failure.

If FCM/APN rejected the push, then refer to respective documentation of push notification error codes.

  • For FCM, refer here
  • For APN, refer here

If Mesibo rejected the push, then the error code will be a value greater than 900. Refer to the table below for special error codes and their meaning:

Error Code Description
901 Push service for the app is not configured
901 Push service for the app is temporarily disabled
903 Push certificate expired
904 Push certificate error
905 Production certificate is required for this push
911 Bad push token
912 Mismatch in APN certificate and the push token app id
913 Bad payload
mesibo, android, ios