How to keep Listening for Push Notifications on Android in the background
Asked Answered
D

2

5

I am working on Push Notifications in Android. Now the issue is that I want to keep running my Push Notifications on the back ground as soon as the app start because I have no idea when the server will push the data to the devices.

The main requirement is that our corporate app is having more than 10 activities and based on the notification received, I have to bring the related activity on the foreground so that user can preform action on that or do some silent action in the background regardless the activity is in foreground.

Can somebody suggest how can I implement this type of requirement. Do I need to do it in a Service.

Thanks

Demurrer answered 14/9, 2012 at 15:6 Comment(0)
R
10

An Android application on an Android device doesn't need to be running to receive messages. The system will wake up the Android application via Intent broadcast when the message arrives, as long as the application is set up with the proper broadcast receiver and permissions.

take look at this;

http://developer.android.com/guide/google/gcm/gcm.html

when message received from gcm server

onMessage(Context context, Intent intent): method of GCMIntentService gets fire,

so you write your code there

take sample example from here

https://github.com/ketanpatel25/GCM-Demo/tree/master/gcm

Rockyrococo answered 14/9, 2012 at 15:31 Comment(4)
The push notification has to have any particular attribute in order to wake up the application? My app doesn't wake up if it doesn't has a "message" attribute. #31213007Crossbeam
It's pretty old but wanted to correct something: Shiva, i don't think what you have mentioned in completely correct. The app process needs to be running on the phone in-order for it to receive the push notification. You can check this by killing the app process and you will not receive any notification for that app.Rehnberg
GCM has been deprecated by GoogleMondragon
Now you need to use FCM(Firebase Cloud Messaging), here is reference link firebase.google.com/docs/cloud-messaging/android/clientRockyrococo
S
2

What you're trying to do defeats the purpose of push notifications. In push notifications, the server sends the message through Google APIs. These APIs then send a broadcast message to your app, which you listen for. Continuously keeping the app open in the background and asking the server for new messages is called polling.

Read up on the GCM documentation. Whenever you receive a message, Android will ca the onMessage(); method of your GCMIntentService.

Stash answered 14/9, 2012 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.