Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a notification in the android status bar?

Hi just wanted to share my notification builder for android the answer is below.

Please share any changes.

like image 206
sherif Avatar asked Nov 04 '22 20:11

sherif


1 Answers

minimal usage :

NotificatorFacade nb = new NotificatorFacade(context);
nb.show(R.drawable.icon, "tickerText", new Date().getTime(), 
                 "contentTitle", "contentText", ERROR_NOTIFICATION_ID);

source:


package my.tools.android.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public class NotificatorBuilder {

    private final Context context;
    private Intent intent;
    private Integer flags;

    private Integer defaults;
    private Uri sound;

    public NotificatorBuilder(Context context) {
        this.context = context;

    }

    /**
     * sets the flags for Notification.defaults
     * 
     * @param defaults
     */
    public void setDefaults(int defaults) {
        this.defaults = defaults;
    }

    /**
     * displays the notification with the given parameters it sets
     * notification.flags|=Notification.FLAG_AUTO_CANCEL when intent (setIntent)
     * is null the setIntent functionality was not tested
     * 
     * @see http
     *      ://developer.android.com/guide/topics/ui/notifiers/notifications.
     *      html
     * @param iconDrawable the icon
     * @param tickerText 
     * @param when 
     * @param contentTitle
     * @param contentText
     * @param NOTIFICATION_ID this id is used for later identification 
     */

    public void show(int iconDrawable, CharSequence tickerText, long when,
            CharSequence contentTitle, CharSequence contentText,
            int NOTIFICATION_ID) {
        // Get a reference to the NotificationManager:
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(ns);
        // Instantiate the Notification:
        Notification notification = new Notification(iconDrawable, tickerText,
                when);
        // Define the Notification's expanded message and Intent:

        if (sound == null) {

            notification.sound = sound;
        }
        if (flags != null) {
            notification.flags = flags;
        }
        if (defaults != null) {
            notification.defaults = defaults;
        }
        // if intent null create one and set the FLAG_AUTO_CANCEL flag EXTENDS
        // FLAGS!!!
        if (intent == null) {
            setIntent(new Intent(context, NotificatorBuilder.class));
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
        }

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                intent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText,

        contentIntent);

        mNotificationManager.notify(NOTIFICATION_ID, notification);

    }

    /**
     * sets the flags for notification usage: NotificatorBuilder nb = new
     * NotificatorBuilder(context);
     * nb.setFlags(Notification.DEFAULT_VIBRATE|Notification.FLAG_INSISTENT);
     * 
     * @param flags
     */
    public void setFlags(int flags) {
        this.flags = flags;
    }

    /**
     * sets the intent for
     * 
     * PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
     * intent, 0); notification.setLatestEventInfo(context, contentTitle,
     * contentText,contentIntent); this functionality was not tested
     * 
     * @param intent
     */
    public void setIntent(Intent intent) {
        this.intent = intent;
    }

    /**
     * sets the sound for the notification was not tested but should work usage
     * for default  notification call the method :setDefaults(Notification.DEFAULT_SOUND);
     * usage: 
     * To use a different sound with your notifications, pass a Uri reference to
     * the sound field. The following example uses a known audio file saved to
     * the device SD card: 
     * notification.sound =
     * Uri.parse("file:///sdcard/notification/ringer.mp3");
     * 
     * In the next example, the audio file is chosen from the internal
     * MediaStore's ContentProvider: notification.sound =
     * Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
     * 
     * @param sound
     */
    public void setSound(Uri sound) {
        this.sound = sound;
    }

}


like image 66
sherif Avatar answered Nov 14 '22 22:11

sherif