Harsha
Harsha

Reputation: 669

How do whatsapp manage to receive push notification even when the app is closed?

I'm currently working on an application which receives text messages (sms) in the background (i.e when the app is closed). I don't know how to notify to the user on status bar from the service that I have registered to receive text message on the background. The NotificationCompat.Builder class requires context as its parameter which we cannot pass in the service class.

How does Whatsapp messenger notify on the status bar when new messages arrive?

I've gone through many similar questions on stackoverflow, but none had a solution for my question. Please help me solve this problem. Any reference or small code will help.

Upvotes: 3

Views: 2820

Answers (1)

arlistan
arlistan

Reputation: 741

You can use a Sticky Service, doing this will make your service start again if your app gets killed:

@Override
 public int onStartCommand(Intent intent, int flags, int startId) {
     Log.i("LocalService", "Received start id " + startId + ": " + intent);
     // We want this service to continue running until it is explicitly
     // stopped, so return sticky.
     return START_STICKY;
}

You can check all the info here:

http://developer.android.com/reference/android/app/Service.html#START_STICKY

Edit:

Here is an example of how to use it:

http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

Upvotes: 2

Related Questions