Example usage for android.app Notification Notification

List of usage examples for android.app Notification Notification

Introduction

In this page you can find the example usage for android.app Notification Notification.

Prototype

@Deprecated
public Notification(int icon, CharSequence tickerText, long when) 

Source Link

Document

Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.

Usage

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

private void SendNotification(String tickerText, String expandedText) {
    NotificationManager notificationManager = (NotificationManager) contextWrapper
            .getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.ateamlogo;
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    notification.flags |= (Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;

    Context context = contextWrapper.getApplicationContext();

    // Intent to launch an activity when the extended text is clicked
    Intent intent2 = new Intent(contextWrapper, SUTAgentAndroid.class);
    PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent2, 0);

    notification.setLatestEventInfo(context, tickerText, expandedText, launchIntent);

    notificationManager.notify(1959, notification);
}

From source file:github.madmarty.madsonic.util.Util.java

public static void showPlayingNotification(final Context context, final DownloadServiceImpl downloadService,
        Handler handler, MusicDirectory.Entry song) {

    // Set the icon, scrolling text and timestamp
    final Notification notification = new Notification(R.drawable.stat_notify, song.getTitle(),
            System.currentTimeMillis());
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

    boolean playing = downloadService.getPlayerState() == PlayerState.STARTED;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        RemoteViews expandedContentView = new RemoteViews(context.getPackageName(),
                R.layout.notification_expanded);
        setupViews(expandedContentView, context, song, playing);
        notification.bigContentView = expandedContentView;
    }//from   w w  w  .j av  a  2  s.  c o  m

    RemoteViews smallContentView = new RemoteViews(context.getPackageName(), R.layout.notification);
    setupViews(smallContentView, context, song, playing);
    notification.contentView = smallContentView;

    Intent notificationIntent = new Intent(context, DownloadActivity.class);
    notification.contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    handler.post(new Runnable() {
        @Override
        public void run() {
            downloadService.startForeground(Constants.NOTIFICATION_ID_PLAYING, notification);
        }
    });

    // Update widget
    MadsonicWidgetProvider.notifyInstances(context, downloadService, true);
}

From source file:es.javocsoft.android.lib.toolbox.ToolBox.java

/**
 * Creates a system notification.//  w  w  w. ja  v  a  2  s  . com
 *    
 * @param context            Context.
 * @param notSound            Enable or disable the sound
 * @param notSoundRawId         Custom raw sound id. If enabled and not set 
 *                         default notification sound will be used. Set to -1 to 
 *                         default system notification.
 * @param multipleNot         Setting to True allows showing multiple notifications.
 * @param groupMultipleNotKey   If is set, multiple notifications can be grupped by this key.
 * @param notAction            Action for this notification
 * @param notTitle            Title
 * @param notMessage         Message
 * @param notClazz            Class to be executed
 * @param extras            Extra information
 * 
 */
public static void notification_generate(Context context, boolean notSound, int notSoundRawId,
        boolean multipleNot, String groupMultipleNotKey, String notAction, String notTitle, String notMessage,
        Class<?> notClazz, Bundle extras, boolean wakeUp) {

    try {
        int iconResId = notification_getApplicationIcon(context);
        long when = System.currentTimeMillis();

        Notification notification = new Notification(iconResId, notMessage, when);

        // Hide the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        if (notSound) {
            if (notSoundRawId > 0) {
                try {
                    notification.sound = Uri.parse("android.resource://"
                            + context.getApplicationContext().getPackageName() + "/" + notSoundRawId);
                } catch (Exception e) {
                    if (LOG_ENABLE) {
                        Log.w(TAG, "Custom sound " + notSoundRawId + "could not be found. Using default.");
                    }
                    notification.defaults |= Notification.DEFAULT_SOUND;
                    notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                }
            } else {
                notification.defaults |= Notification.DEFAULT_SOUND;
                notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            }
        }

        Intent notificationIntent = new Intent(context, notClazz);
        notificationIntent.setAction(notClazz.getName() + "." + notAction);
        if (extras != null) {
            notificationIntent.putExtras(extras);
        }

        //Set intent so it does not start a new activity
        //
        //Notes:
        //   - The flag FLAG_ACTIVITY_SINGLE_TOP makes that only one instance of the activity exists(each time the
        //      activity is summoned no onCreate() method is called instead, onNewIntent() is called.
        //  - If we use FLAG_ACTIVITY_CLEAR_TOP it will make that the last "snapshot"/TOP of the activity it will 
        //     be this called this intent. We do not want this because the HOME button will call this "snapshot". 
        //     To avoid this behaviour we use FLAG_ACTIVITY_BROUGHT_TO_FRONT that simply takes to foreground the 
        //     activity.
        //
        //See http://developer.android.com/reference/android/content/Intent.html           
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        int REQUEST_UNIQUE_ID = 0;
        if (multipleNot) {
            if (groupMultipleNotKey != null && groupMultipleNotKey.length() > 0) {
                REQUEST_UNIQUE_ID = groupMultipleNotKey.hashCode();
            } else {
                if (random == null) {
                    random = new Random();
                }
                REQUEST_UNIQUE_ID = random.nextInt();
            }
            PendingIntent.getActivity(context, REQUEST_UNIQUE_ID, notificationIntent,
                    PendingIntent.FLAG_ONE_SHOT);
        }

        notification.setLatestEventInfo(context, notTitle, notMessage, intent);

        //This makes the device to wake-up is is idle with the screen off.
        if (wakeUp) {
            powersaving_wakeUp(context);
        }

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        //We check if the sound is disabled to enable just for a moment
        AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int previousAudioMode = amanager.getRingerMode();
        ;
        if (notSound && previousAudioMode != AudioManager.RINGER_MODE_NORMAL) {
            amanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        notificationManager.notify(REQUEST_UNIQUE_ID, notification);

        //We restore the sound setting
        if (previousAudioMode != AudioManager.RINGER_MODE_NORMAL) {
            //We wait a little so sound is played
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
            }
        }
        amanager.setRingerMode(previousAudioMode);

        Log.d(TAG, "Android Notification created.");

    } catch (Exception e) {
        if (LOG_ENABLE)
            Log.e(TAG, "The notification could not be created (" + e.getMessage() + ")", e);
    }
}

From source file:com.zertinteractive.wallpaper.MainActivity.java

@SuppressLint("NewApi")
public void setNotification() {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

    @SuppressWarnings("deprecation")
    Notification notification = new Notification(R.drawable.ic_launcher, "Ticker Text",
            System.currentTimeMillis());

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.romantic_wallpaper);

    //the intent that is started when the notification is clicked (works)
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;
    //        notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.flags = Notification.FLAG_LOCAL_ONLY;

    //this is the intent that is supposed to be called when the button is clicked
    Intent switchIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
    ////from  w  w w. j  av a 2  s .c  o m
    notificationView.setOnClickPendingIntent(R.id.download_notification, pendingSwitchIntent);
    notificationManager.notify(1, notification);
}

From source file:org.restcomm.app.qoslib.Services.Intents.IntentHandler.java

private void postSurvey(int surveyid) {
    int icon = 0;
    int customIcon = (owner.getResources().getInteger(R.integer.CUSTOM_NOTIFIER));
    if (customIcon == 0)
        icon = R.drawable.ic_stat_mmcactive;
    else/*  www .j  av  a 2 s  .  c o m*/
        icon = R.drawable.ic_stat_notification_icon;

    if (surveyid > 0) {

        int curr_id = PreferenceManager.getDefaultSharedPreferences(owner).getInt("surveyid", 0);

        if (curr_id == surveyid)
            return;
        //resultIntent.putExtra("id", surveyid);
        requestQuestions(surveyid);
    } else { //default survey id = 1
        //resultIntent.putExtra("id",1);
        requestQuestions(1);
    }

    String message = owner.getString(R.string.survey_notification);
    NotificationManager notificationManager = (NotificationManager) owner
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    Intent notificationIntent = new Intent();
    notificationIntent.setComponent(
            new ComponentName(owner.getPackageName(), "com.cortxt.app.mmcui.Activities.SatisfactionSurvey"));
    notificationIntent.putExtra("id", surveyid);
    notificationIntent.setData((Uri.parse("foobar://" + SystemClock.elapsedRealtime())));
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    int MMC_SURVEY_NOTIFICATION = 8011;
    PendingIntent pendingIntent = PendingIntent.getActivity(owner, MMC_SURVEY_NOTIFICATION + surveyid,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(owner, message, message, pendingIntent);
    notificationManager.notify(MMC_SURVEY_NOTIFICATION, notification);
}

From source file:com.google.android.gms.common.GooglePlayServicesUtil.java

public static void showErrorNotification(int errorCode, Context context) {
    Resources resources = context.getResources();
    Notification notification = new Notification(17301642,
            resources.getString(C0192R.string.common_google_play_services_notification_ticker),
            System.currentTimeMillis());
    notification.flags |= 16;/*from ww w.  j  a  v a 2s .c  om*/
    CharSequence e = m116e(context, errorCode);
    String u = m119u(context);
    notification.setLatestEventInfo(context, e,
            resources.getString(C0192R.string.common_google_play_services_error_notification_requested_by_msg,
                    new Object[] { u }),
            getErrorPendingIntent(errorCode, context, 0));
    ((NotificationManager) context.getSystemService("notification")).notify(39789, notification);
}

From source file:org.mariotaku.harmony.MusicPlaybackService.java

private void startSleepTimer(long milliseconds, boolean gentle) {

    Calendar now = Calendar.getInstance();
    mCurrentTimestamp = now.getTimeInMillis();
    mStopTimestamp = mCurrentTimestamp + milliseconds;

    int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL
            | DateUtils.FORMAT_CAP_AMPM;

    format_flags |= DateUtils.FORMAT_SHOW_TIME;
    String time = DateUtils.formatDateTime(this, mStopTimestamp, format_flags);

    CharSequence contentTitle = getString(R.string.sleep_timer_enabled);
    CharSequence contentText = getString(R.string.notification_sleep_timer, time);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    Notification notification = new Notification(R.drawable.ic_stat_playback, null, 0);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.icon = R.drawable.ic_stat_sleeptimer;
    notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

    mGentleSleepTimer = gentle;/*from w ww.  ja va2  s  .c o m*/
    mNotificationManager.notify(ID_NOTIFICATION_SLEEPTIMER, notification);
    mSleepTimerHandler.sendEmptyMessageDelayed(START_SLEEP_TIMER, milliseconds);
    final int nmin = (int) milliseconds / 60 / 1000;
    Toast.makeText(this, getResources().getQuantityString(R.plurals.NNNminutes_notif, nmin, nmin),
            Toast.LENGTH_SHORT).show();
}

From source file:com.wso2.mobile.mdm.services.Operation.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//* w  w  w .j av a2 s .  c  o  m*/
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, NotifyActivity.class);
    notificationIntent.putExtra("notification", message);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notificationManager.notify(0, notification);
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

From source file:org.restcomm.app.qoslib.Services.LibPhoneStateListener.java

public void popupDropped(final EventType droptype, final int rating, final int evtId) {
    if (rating == 0)
        return;//  www.  j  a  va2  s .  c o  m
    owner.handler.post(new Runnable() {
        // @Override
        public void run() {
            String message = "";
            int icon;
            icon = R.drawable.ic_stat_dropped;
            String title = "";
            String msg = "";

            // server can specify whether a confirmation can be invoked on a low rated potentially-dropped call
            int allowConfirm = PreferenceManager.getDefaultSharedPreferences(owner)
                    .getInt(PreferenceKeys.Miscellaneous.ALLOW_CONFIRMATION, 5);
            String noConfirm = (owner.getResources().getString(R.string.NO_CONFIRMATION));
            int allowPopup = PreferenceManager.getDefaultSharedPreferences(owner)
                    .getInt(PreferenceKeys.Miscellaneous.ALLOW_DROP_POPUP, 2);
            if (allowPopup == 1 && !owner.getUseRadioLog())
                allowPopup = 0;
            if (allowPopup == 0)
                return;

            if (noConfirm.equals("1"))
                allowConfirm = 0;
            if (allowConfirm > 0 && rating < allowConfirm && rating < 4) // if confirmation allow, must be above threshold or high rating dropped call
                return;
            else if (allowConfirm == 0 && rating < 4) // drop call silently if marginal with no confirmation
                return;
            // allowConfirm>=5 disables the confirmation because rating always <= 5
            // allowConfirm=1 hits the 'else' and invokes confirmation if rating >= 1 and <5
            // allowConfirm=3 hits the 'else' and invokes confirmation if rating >= 3 and <5
            int expiry = 60000 * 2 * 60;
            int customText = (owner.getResources().getInteger(R.integer.CUSTOM_EVENTNAMES));
            message = owner.getString((customText == 1) ? R.string.sharecustom_speedtest_wifi
                    : R.string.sharemessage_speedtest_wifi);

            if (rating >= 5 || allowConfirm == 0) {
                title = Global.getAppName(owner);
                msg = "mmc detected ";
                if (droptype == EventType.EVT_CALLFAIL)
                    message = owner.getString((customText == 1) ? R.string.Custom_Notification_call_failed
                            : R.string.MMC_Notification_call_failed);
                else
                    message = owner.getString((customText == 1) ? R.string.Custom_Notification_call_dropped
                            : R.string.MMC_Notification_call_dropped);
                message += ": " + owner.getString(R.string.MMC_Notification_view_event);
                msg += message;
            } else if (rating >= allowConfirm && rating > 1) {
                if (droptype == EventType.EVT_CALLFAIL) {
                    title = owner.getString((customText == 1) ? R.string.Custom_Notification_did_you_fail
                            : R.string.MMC_Notification_did_you_fail);
                    message = owner.getString((customText == 1) ? R.string.Custom_Notification_did_failed
                            : R.string.MMC_Notification_did_failed);
                } else if (droptype == EventType.EVT_DROP) {
                    title = owner.getString((customText == 1) ? R.string.Custom_Notification_did_you_drop
                            : R.string.Custom_Notification_did_dropped);
                    message = owner.getString((customText == 1) ? R.string.MMC_Notification_did_dropped
                            : R.string.MMC_Notification_did_dropped);
                } else if (droptype == EventType.EVT_DISCONNECT || droptype == EventType.EVT_UNANSWERED) {
                    expiry = 60000;
                    icon = R.drawable.ic_stat_disconnect;
                    title = owner.getString((customText == 1) ? R.string.Custom_Notification_did_you_disconnect
                            : R.string.MMC_Notification_did_you_disconnect);
                    message = owner.getString((customText == 1) ? R.string.Custom_Notification_did_disconnect
                            : R.string.MMC_Notification_did_disconnect);
                }
                msg = message;
            }

            java.util.Date date = new java.util.Date();
            String time = date.toLocaleString();
            msg += " at " + time;
            //Toast toast = Toast.makeText(MainService.this, msg, Toast.LENGTH_LONG);
            //toast.show();

            NotificationManager notificationManager = (NotificationManager) owner
                    .getSystemService(Service.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, System.currentTimeMillis());
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            //Intent notificationIntent = new Intent(MainService.this, Dashboard.class);
            Intent notificationIntent = new Intent();//, "com.cortxt.app.mmcui.Activities.Dashboard");
            notificationIntent.setClassName(owner, "com.cortxt.app.uilib.Activities.Dashboard");
            notificationIntent.putExtra("eventId", evtId);

            notificationIntent.setData((Uri.parse("foobar://" + SystemClock.elapsedRealtime())));
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(owner, MMC_DROPPED_NOTIFICATION + evtId,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            notification.setLatestEventInfo(owner, title, message, pendingIntent);
            notificationManager.notify(MMC_DROPPED_NOTIFICATION, notification);
            long expirytime = System.currentTimeMillis() + expiry;
            PreferenceManager.getDefaultSharedPreferences(owner).edit()
                    .putLong(PreferenceKeys.Monitoring.NOTIFICATION_EXPIRY, expirytime).commit();

        }
    });

}

From source file:com.free.searcher.MainFragment.java

private void setMood(int moodId, String text, boolean showTicker) {
    //        // In this sample, we'll use the same text for the ticker and the expanded notification
    //        CharSequence text = getText(textId);
    ///*from   w ww.  ja  v  a2s.c  om*/
    //        // choose the ticker text
    String tickerText = showTicker ? text : null;

    // Set the icon, scrolling text and timestamp
    Notification notification = new Notification(moodId, tickerText, System.currentTimeMillis());

    // Set the info for the views that show in the notification panel.
    //        notification.setLatestEventInfo(activity, "Searching finished",
    //                              text, makeMoodIntent(moodId));
    notification.defaults = Notification.DEFAULT_ALL;
    // Send the notification.
    // We use a layout id because it is a unique number.  We use it later to cancel.
    mNotificationManager.notify(MOOD_NOTIFICATIONS, notification);
}