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

public Notification() 

Source Link

Document

Constructs a Notification object with default values.

Usage

From source file:Main.java

public static NotificationManager showNotification(Context context, String title, String message,
        String alertmessage, int icon, Class<?> forwordActivity) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // The PendingIntent to launch our activity if the user selects this
    // notification
    // PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    // new Intent(context, forwordActivity), 0);
    // construct the Notification object.
    Notification noti = new Notification();
    noti.icon = icon;// www.  ja  va2 s . c  o m
    noti.tickerText = alertmessage;
    noti.when = System.currentTimeMillis();

    // Set the info for the views that show in the notification panel.
    // noti.setLatestEventInfo(context, title, message, contentIntent);

    // after a 100ms delay, vibrate for 250ms, pause for 100 ms and
    // then vibrate for 500ms.
    noti.vibrate = new long[] { 100, 250, 100, 500 };

    // Note that we use R.layout.incoming_message_panel as the ID for
    // the notification. It could be any integer you want, but we use
    // the convention of using a resource id for a string related to
    // the notification. It will always be a unique number within your
    // application.
    nm.notify(1, noti);
    return nm;
}

From source file:com.google.android.apps.location.gps.gnsslogger.TimerService.java

@Override
public IBinder onBind(Intent intent) {
    Notification notification = new Notification();
    startForeground(NOTIFICATION_ID, notification);
    return mBinder;
}

From source file:com.fn.reunion.app.xmpp.Notifier.java

public void notify(String notificationId, String apiKey, String title, String message, String uri) {
    Log.d(LOGTAG, "notify()...");

    Log.d(LOGTAG, "notificationId=" + notificationId);
    Log.d(LOGTAG, "notificationApiKey=" + apiKey);
    Log.d(LOGTAG, "notificationTitle=" + title);
    Log.d(LOGTAG, "notificationMessage=" + message);
    Log.d(LOGTAG, "notificationUri=" + uri);

    if (isNotificationEnabled()) {
        // Show the toast
        if (isNotificationToastEnabled()) {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }/*  www  . ja  v  a 2  s  .c o m*/

        Notification notification = new Notification();
        notification.icon = getNotificationIcon();
        notification.defaults = Notification.DEFAULT_LIGHTS;

        if (isNotificationSoundEnabled()) {
            notification.defaults |= Notification.DEFAULT_SOUND;
        }

        if (isNotificationVibrateEnabled()) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.when = System.currentTimeMillis();
        notification.tickerText = message;

        Intent intent = new Intent(context, NotificationDetailsActivity.class);
        intent.putExtra(Constants.NOTIFICATION_ID, notificationId);
        intent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
        intent.putExtra(Constants.NOTIFICATION_TITLE, title);
        intent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
        intent.putExtra(Constants.NOTIFICATION_URI, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

        /*      notification.setLatestEventInfo(context, notficationMessage , message , contentIntent);
              notificationManager.notify(Consts.NOTIFICATION_ID, notification);*/

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(getNotificationIcon()).setContentTitle("").setContentText("");

        Notification n = mBuilder.setContentIntent(contentIntent).setSmallIcon(getNotificationIcon())
                .setTicker("").setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message.replaceAll("[\t\n\r]", "\n")))
                .setContentText(message).build();

        if (isNotificationSoundEnabled()) {
            n.defaults |= Notification.DEFAULT_SOUND;
        }

        if (isNotificationVibrateEnabled()) {
            n.defaults |= Notification.DEFAULT_VIBRATE;
        }
        notificationManager.notify(Consts.NOTIFICATION_ID, n);
        // Notification
    } else {
        Log.w(LOGTAG, "Notificaitons disabled.");
    }
}

From source file:jp.maju.wifiserver.client.ClientService.java

@Override
public void onCreate() {
    super.onCreate();

    startForeground(NOTIFICATION_ID, new Notification());

    // mMessageHandler = new MessageHandler(this);
    mClientObserver = new ConnectionObserver();
    mClientObserver.setOnStateListener(this);

    registerReceiver(mClientObserver, CommonUtil.getNewIntentFilterWillBeRecieved());
}

From source file:com.perm.DoomPlay.DownloadNotifBuilder.java

private Notification createStartingNew() {
    Notification notification = new Notification();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notif_download);

    views.setProgressBar(R.id.progressDownload, 100, 0, true);
    views.setTextViewText(R.id.notifTitle, context.getResources().getString(R.string.Downloading));
    views.setTextViewText(R.id.notifArtist, track.getArtist() + "-" + track.getTitle());
    views.setImageViewResource(R.id.notifPause, R.drawable.widget_pause);

    ComponentName componentName = new ComponentName(context, DownloadingService.class);

    Intent intentClose = new Intent(PlayingService.actionClose);
    intentClose.putExtra("aid", track.getAid());
    intentClose.setComponent(componentName);

    Intent intentPause = new Intent(PlayingService.actionIconPause);
    intentPause.putExtra("aid", track.getAid());
    intentPause.setComponent(componentName);

    views.setOnClickPendingIntent(R.id.notifClose,
            PendingIntent.getService(context, notificationId, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));

    views.setOnClickPendingIntent(R.id.notifPause,
            PendingIntent.getService(context, notificationId, intentPause, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = views;/*from w  ww .  j ava 2 s . c  o  m*/
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.icon = R.drawable.download_icon;

    return notification;

}

From source file:com.molice.oneingdufs.androidpn.Notifier.java

public void notify(String notificationId, String apiKey, String title, String message, String uri) {
    Log.d(LOGTAG, "notify()...");

    Log.d(LOGTAG, "notificationId=" + notificationId);
    Log.d(LOGTAG, "notificationApiKey=" + apiKey);
    Log.d(LOGTAG, "notificationTitle=" + title);
    Log.d(LOGTAG, "notificationMessage=" + message);
    Log.d(LOGTAG, "notificationUri=" + uri);

    if (SettingsActivity.getNotificationEnabled(context)) {
        // Show the toast
        //            if (isNotificationToastEnabled()) {
        //                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        //            }

        // Notification
        Notification notification = new Notification();
        notification.icon = getNotificationIcon();
        //            notification.defaults = Notification.DEFAULT_LIGHTS;
        if (SettingsActivity.getNotificationSound(context)) {
            notification.defaults |= Notification.DEFAULT_SOUND;
        }/*from   w  w w. ja v  a2s  . c o m*/
        if (SettingsActivity.getNotificationVibrate(context)) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.when = System.currentTimeMillis();
        notification.tickerText = message;

        //            Intent intent;
        //            if (uri != null
        //                    && uri.length() > 0
        //                    && (uri.startsWith("http:") || uri.startsWith("https:")
        //                            || uri.startsWith("tel:") || uri.startsWith("geo:"))) {
        //                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        //            } else {
        //                String callbackActivityPackageName = sharedPrefs.getString(
        //                        Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");
        //                String callbackActivityClassName = sharedPrefs.getString(
        //                        Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
        //                intent = new Intent().setClassName(callbackActivityPackageName,
        //                        callbackActivityClassName);
        //                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //            }

        Intent intent = new Intent(context, MessageDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        JSONObject data = formatMetaFromTitle(title);
        try {
            data.putOpt("id", notificationId);
            data.putOpt("content", message);
        } catch (Exception e) {
            Log.d("JSON", "Notifier#notify, e=" + e.toString());
        }
        intent.putExtra("data", data.toString());
        intent.putExtra("fromNotification", true);
        //            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //            intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        //            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        //            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, random.nextInt(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setLatestEventInfo(context, data.optString("title"), message, contentIntent);
        notificationManager.notify(random.nextInt(), notification);

        //            Intent clickIntent = new Intent(
        //                    Constants.ACTION_NOTIFICATION_CLICKED);
        //            clickIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
        //            clickIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
        //            clickIntent.putExtra(Constants.NOTIFICATION_TITLE, title);
        //            clickIntent.putExtra(Constants.NOTIFICATION_MESSAGE, message);
        //            clickIntent.putExtra(Constants.NOTIFICATION_URI, uri);
        //            //        positiveIntent.setData(Uri.parse((new StringBuilder(
        //            //                "notif://notification.adroidpn.org/")).append(apiKey).append(
        //            //                "/").append(System.currentTimeMillis()).toString()));
        //            PendingIntent clickPendingIntent = PendingIntent.getBroadcast(
        //                    context, 0, clickIntent, 0);
        //
        //            notification.setLatestEventInfo(context, title, message,
        //                    clickPendingIntent);
        //
        //            Intent clearIntent = new Intent(
        //                    Constants.ACTION_NOTIFICATION_CLEARED);
        //            clearIntent.putExtra(Constants.NOTIFICATION_ID, notificationId);
        //            clearIntent.putExtra(Constants.NOTIFICATION_API_KEY, apiKey);
        //            //        negativeIntent.setData(Uri.parse((new StringBuilder(
        //            //                "notif://notification.adroidpn.org/")).append(apiKey).append(
        //            //                "/").append(System.currentTimeMillis()).toString()));
        //            PendingIntent clearPendingIntent = PendingIntent.getBroadcast(
        //                    context, 0, clearIntent, 0);
        //            notification.deleteIntent = clearPendingIntent;
        //
        //            notificationManager.notify(random.nextInt(), notification);

    } else {
        Log.w(LOGTAG, "Notificaitons disabled.");
    }
}

From source file:com.open.file.manager.CutCopyService.java

/**
 * Notify some error has occurred with notification
 * @param errorRes resource to show//from  ww  w.  j a v  a2s.  c o  m
 */
private void notifyError(int errorRes) {
    String errformat, errstring;
    stopForeground(true);
    errformat = getResources().getString(errorRes);
    errstring = String.format(errformat, getResources().getString(actions[currentaction]));
    cutcopynotification = new Notification();
    cutcopynotification.contentView = new RemoteViews(getApplicationContext().getPackageName(),
            R.layout.errornot);
    cutcopynotification.contentView.setTextViewText(R.id.errortext, errstring);
    cutcopynotification.contentIntent = contentIntent;
    cutcopynotification.icon = R.drawable.error;
    cutcopymanager.notify(completeid, cutcopynotification);
}

From source file:org.peercast.core.PeerCastService.java

private Notification createNotification() {
    if (activeChInfos.isEmpty()) {
        //?????????
        isShowNotification = false;//from   www . jav  a 2  s.co m
        return new Notification();
    }

    Stats stats = Stats.fromNativeResult(nativeGetStats());

    String title = "PeerCast";
    String text = "..";
    Intent intent = new Intent();
    //???????????
    for (ChannelInfo info : activeChInfos.values()) {
        title = "Playing: " + info.getName();
        text = info.getDesc();
        text += "  ";
        text += info.getComment();
        if (info.getUrl() != null) {
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(info.getUrl()));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        break;
    }

    String info = String.format("D %.1f / U %.1f kbs ", stats.getInBytes() / 1024f * 8,
            stats.getOutBytes() / 1024f * 8);
    info += String.format("[%d / %d MB]", stats.getInTotalBytes() / 1024 / 1024,
            stats.getOutTotalBytes() / 1024 / 1024);

    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_notify_icon);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

    return notificationBuilder.setContentTitle(title).setContentText(text).setContentInfo(info)
            .setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_notify_icon).setLargeIcon(icon).build();
}

From source file:com.csipsimple.service.SipNotifications.java

@SuppressWarnings("deprecation")
private void searchNotificationPrimaryText(Context aContext) {
    try {//from w ww .  j av  a  2  s . c  o m
        Notification ntf = new Notification();
        ntf.setLatestEventInfo(aContext, TO_SEARCH, "", null);
        LinearLayout group = new LinearLayout(aContext);
        ViewGroup event = (ViewGroup) ntf.contentView.apply(aContext, group);
        recurseSearchNotificationPrimaryText(event);
        group.removeAllViews();
    } catch (Exception e) {
        Log.e(THIS_FILE, "Can't retrieve the color", e);
    }
}

From source file:com.perm.DoomPlay.DownloadNotifBuilder.java

public Notification createPaused() {
    Notification notification = new Notification();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notif_download);

    views.setProgressBar(R.id.progressDownload, 100, 0, false);
    views.setTextViewText(R.id.notifTitle, context.getResources().getString(R.string.paused));
    views.setTextViewText(R.id.notifArtist, track.getArtist() + "-" + track.getTitle());
    views.setImageViewResource(R.id.notifPause, R.drawable.widget_play);

    Intent intentClose = new Intent(PlayingService.actionClose);
    intentClose.putExtra("aid", track.getAid());
    intentClose.setComponent(new ComponentName(context, DownloadingService.class));

    Intent intentPause = new Intent(PlayingService.actionIconPlay);
    intentPause.putExtra("aid", track.getAid());
    intentPause.setComponent(new ComponentName(context, DownloadingService.class));

    views.setOnClickPendingIntent(R.id.notifClose,
            PendingIntent.getService(context, notificationId, intentClose, PendingIntent.FLAG_UPDATE_CURRENT));

    views.setOnClickPendingIntent(R.id.notifPause,
            PendingIntent.getService(context, notificationId, intentPause, PendingIntent.FLAG_UPDATE_CURRENT));

    notification.contentView = views;/*ww  w . j a va2  s .  com*/
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.icon = R.drawable.download_icon;

    return notification;
}