Adding Notifications, Reading Notifications, Getting number of notifications in Android M.

By | February 20, 2016

This demo will show how to

1. Create and add a new Notification,
2. How many notifications are active in the current Application?
3. Delegate method for Notification Deletion.

Infolinks

For Adding a Notification we can use the following snippet.



// Create a Notification and notify the system.
final Notification.Builder builder = new Notification.Builder(getActivity())
	.setSmallIcon(R.mipmap.ic_notification)
	.setContentTitle(getString(R.string.app_name))
	.setContentText(getString(R.string.sample_notification_content))
	.setAutoCancel(true)
	.setDeleteIntent(mDeletePendingIntent);

final Notification notification = builder.build();
mNotificationManager.notify(++mNotificationId, notification);

    

For reading the number of currently Active notifications we can use



// Query the currently displayed notifications.
final StatusBarNotification[] activeNotifications = mNotificationManager
	.getActiveNotifications();
final int numberOfNotifications = activeNotifications.length;
mNumberOfNotifications.setText(getString(R.string.active_notifications,
	numberOfNotifications));

    

To know when a Notification is deleted.



 // Create a PendingIntent to be fired upon deletion of a Notification.
 Intent deleteIntent = new Intent(ActiveNotificationActivity.ACTION_NOTIFICATION_DELETE);
 mDeletePendingIntent = PendingIntent.getBroadcast(getActivity(),
                2323 /* requestCode */, deleteIntent, 0);


Infolinks

ActiveNotificationActivity.java (Activity when notification is clicked).



package com.example.android.activenotifications;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;

public class ActiveNotificationActivity extends MainActivity {

    private ActiveNotificationFragment mFragment;

    protected static final String ACTION_NOTIFICATION_DELETE
            = "com.example.android.activenotifications.delete";

    private BroadcastReceiver mDeleteReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mFragment == null) {
                findFragment();
            }
            mFragment.updateNumberOfNotifications();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        findFragment();
        mFragment.updateNumberOfNotifications();
    }

    private void findFragment() {
        mFragment = (ActiveNotificationFragment) getSupportFragmentManager()
                .findFragmentById(R.id.sample_content_fragment);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mDeleteReceiver, new IntentFilter(ACTION_NOTIFICATION_DELETE));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mDeleteReceiver);
    }
}


You can download the complete source Code from here.

Infolinks

Leave a Reply

Your email address will not be published.