Iqraa
Iqraa

Reputation: 445

How to get Push notification in android when app is in background?

I am developing an android application in which the user gets login and can view the messages received in their inbox (each message contain time, date, subject and sender information that is already stored in my database). There is a inbox button that shows the number of messages received in inbox as a badge notification like if there are 10 messages present in inbox 10 is shown as a text in badge notification. I am succeed in doing all so but I want that whenever user gets any message in its inbox; the user will be notified through push notification either app is opened or is in background. I have searched a lot but I have no idea from where to start. Every kind of help is appreciated! Thanks in advance.

Upvotes: 3

Views: 3169

Answers (2)

Yupi
Yupi

Reputation: 4470

You can approach to your issue at least on two ways.

First way is to implement AlarmManager which is going to start service which is going to check is new message arrived and if it is you can rise notification. But some limitations of this approach are that you have to setup AlaramManager to trigger services once a while for example each 15 min and if in meanwhile message arrives user will not be notified in realtime. Actualy you could run services continusly but that would be bad practice as it would speed up battery usage and services could be killed by OS if freespace needed.

Second approach is to use some third part client like FCM which is free but with limitation to 4kb per message(notification message) which is actualy pretty enough for notification. Often messages(notification message) are sent manualy from Firebase console but second limitation if you want to rise notification when user is offline you have to include data payload to trigger onMessageReceived and rise notification. If you want to do that when app is in background for that you need custom server side logic as that is not possible from firebase console. Luckly for you there are tutorials how to do that. This tutorial will help you to setup FCM: https://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

And here you can find custom PHP server for sending messages: http://demo.androidhive.info/firebase/notifications/

Upvotes: 2

mac229
mac229

Reputation: 4869

You can use Firebase Messaging

Upvotes: 0

Related Questions