26 Sep

Android NotificationManager

Share Button

Notifications are small icon on the status bar intended for user to receive information. This alerts the user for gaining immediate attention. For raising notifications in Android, use NotificationManager. We must get the service via getSystemService(Notification_Service) from the activity. NotificationManager has three methods: i) notify() ii) cancel() iii) cancelAll(). Different types of notifications are like Hardware notification such as Flashing LED on device, generating a sound, vibrate and Icons that will display another activity.Service has to be defined in the android manifest. First call startService(), that takes Intent as parameter. onStartCommand() is called each time the service is sent a command via startService(). This Service will keep running till we call stopService(), that has intent as parameter.

Setting Receivers: Sometimes we would like to trigger some service rather than an activity. Android offers receiver. This can be implemented using BroadcastReceiver interface. These are disposable objects to receive broadcast intents.

Use Pending intent which is a token given to foreign application e.g AlarmManager, NotificationManager access to execute a code in the android application.

Here is a sample demo of notification usage in android phone.

Layout xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#ffff8c05″
><Button
android:id=”@+id/startservice”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:background=”#ff2d4c9b”
android:textColor=”#fffafff4″
android:text=”Start Service Notification”
/>
<Button
android:id=”@+id/stopservice”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:background=”#ff2d4c9b”
android:textColor=”#fffafff4″

android:text=”Stop Service”
/>
</LinearLayout>

 

NotifyActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class NotifyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStartService = (Button)findViewById(R.id.startservice);
Button buttonStopService = (Button)findViewById(R.id.stopservice);

buttonStartService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(NotifyActivity.this, NotifyService.class);
NotifyActivity.this.startService(intent);
}});

buttonStopService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra(NotifyService.STOP_SERVICE_BROADCAST_KEY, NotifyService.RQS_STOP_SERVICE);
sendBroadcast(intent);
}});

}
}

}});

buttonStopService.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra(NotifyService.STOP_SERVICE_BROADCAST_KEY, NotifyService.RQS_STOP_SERVICE);
sendBroadcast(intent);
}});

}
}

NotifyService.java

 

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;public class NotifyService extends Service {

final static String ACTION = “NotifyServiceAction”;
final static String STOP_SERVICE_BROADCAST_KEY=”StopServiceBroadcastKey”;
final static int RQS_STOP_SERVICE = 1;

NotifyServiceReceiver notifyServiceReceiver;

 

private final String myBlog = “http://buddingcoder.com/”;

@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);

// Send Notification
Context context = getApplicationContext();
String notificationTitle = “Demo of Notification!”;
String notificationText = “Android Website”;
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent
= PendingIntent.getActivity(getBaseContext(),
0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification.Builder(this)
.setContentTitle(notificationTitle)
.setContentText(notificationText).setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, notification);

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

public class NotifyServiceReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra(STOP_SERVICE_BROADCAST_KEY, 0);

if (rqs == RQS_STOP_SERVICE){
stopSelf();
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.cancelAll();
}
}
}

}

image

Share Button