It is now possible to send push notifications from Azure Mobile Center.

One usual reason why we need to send notifications to our users is to encourage them to use the app on a regular basis. Obviously, in this scenario, we will not have a person whose job is to manually send notifications to each user from the portal. We need an automated process. So even though it’s possible to use the portal, it will be the work of our services to automatically send them.

Mobile application configuration

For this post I assume that you already have your application able to receive push notifications. Please note that Azure Mobile Center utilise Firebase (FCM) for Android and therefore we are perfectly up to date !

Here you will find instructions to correctly configure your apps :

The API

API First

One of the main assets for Azure Mobile Center is the very way it is developed. The Api developed before the web portal is added. It means that everything that can be done from the portal, can be done with the Api in REST !

The documentation for these APIs is available as a Swagger here https://docs.microsoft.com/en-us/appcenter/api-docs/ so it is possible to directly test them.

In order to access the Api you need to create an access token on https://mobile.azure.com/settings/apitokens. Store the created token in a safe place as you will never be able to see it again !

Azure Mobile Center API token creation

As I write these lines, the Push API is not documented in the correct place but in the Analytics part. At first, as I did not find it, I used CharlesProxy to analyse the generated network calls and as expected it was very easy to use the API this way !

Sending push notifications using Mobile Center API is very easy and convenient. As it is a REST based API, it is easy to add push to any webservice using any language.

Getting the user

In order to send push notifications you need to get your user name. For this, you just need to call the following service with a GET query

https://api.mobile.azure.com/v0.1/user

User API response

Getting the application

In order to send push notifications you need to get your application name. For this, you just need to call the following service with a GET query

https://api.mobile.azure.com/v0.1/apps

Apps API response

Sending a push notification

Now that we have to necessary piece of information, sending a push notification is just a POST query on the right url :

https://api.mobile.azure.com/v0.1/apps/[username]/[applicationname]/push/notifications

The push request payload will be in json, so we need to add the correct HTTP header :

Content-Type: application/json

We just need to add the payload in the HTTP request’s body :

{
	"notification_target": null,
	"notification_content": {
		"name": "Test",
		"title": "Coucou Monde !",
		"body": "Test depuis Postman !",
		"custom_data": {}
	}
}

Now send it !

Push API response

Conclusion

In this post we saw how to send a push notification to all the users of an application using Azure Mobile Center’s REST API. It is possible to filter these users be it is a story for another ti,e.

Today, my life as a developer became easy thanks to Azure Mobile Center. Don’t hesitate, like I do, to follow any Azure Mobile Center update because sometimes it is really worth your time :)

Comments