Example usage for android.app NotificationManager notify

List of usage examples for android.app NotificationManager notify

Introduction

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

Prototype

public void notify(int id, Notification notification) 

Source Link

Document

Post a notification to be shown in the status bar.

Usage

From source file:com.github.notizklotz.derbunddownloader.download.IssueDownloadService.java

private void notifyUser(CharSequence contentTitle, CharSequence contentText, int icon) {
    Notification.Builder mBuilder = new Notification.Builder(getApplicationContext()).setSmallIcon(icon)
            .setContentTitle(contentTitle).setContentText(contentText).setTicker(contentTitle)
            .setAutoCancel(true);//from  w w w .j a v a  2s. c  o  m

    //http://developer.android.com/guide/topics/ui/notifiers/notifications.html
    // The stack builder object will contain an artificial back stack for thestarted Activity.
    // This ensures that navigating backward from the Activity leads out of your application to the Home screen.
    mBuilder.setContentIntent(android.support.v4.app.TaskStackBuilder.create(getApplicationContext())
            .addParentStack(DownloadedIssuesActivity_.class)
            .addNextIntent(new Intent(getApplicationContext(), DownloadedIssuesActivity_.class))
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //noinspection deprecation
    mNotifyMgr.notify(1, mBuilder.getNotification());
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.service.OrgSyncService.java

private void notifyError() {
    NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(this)
            // TODO hardcoded
            .setContentTitle("Could not access files").setContentText("Please change directory")
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, PrefsActivity.class),
                    PendingIntent.FLAG_UPDATE_CURRENT));

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(237388, notBuilder.build());
}

From source file:com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java

public void showNotification(Bundle settings, Intent intent) {
    Uri uri = intent.getData();/*from  w w w . j av  a  2s.c  om*/
    int uriCode = URI_MATCHER.match(uri);

    if (uriCode != C.REQUEST_CODE_LOCALNOTIFICATION) {
        Log.w(C.TAG, "Notification with invalid uri: " + uri);
        return;
    }

    String idString = uri.getLastPathSegment();
    if (idString == null) {
        Log.w(C.TAG, "Just got a notification with no data");
        return;
    }

    Log.i(C.TAG, "LocalNotificationsBackend.showNotification: " + settings + " keys: " + settings.keySet());

    String key = PREFS_PREFIX + idString;
    JSONObject obj = getJSONNotificationObject(key);
    if (obj == null) {

        // return;
        obj = new JSONObject();
        try {
            obj.put("vibrate", false);
            obj.put("sound", false);
            obj.put("title", "A test notification");
            obj.put("body", "New job: fix Mr. Gluck's hazy TV, PDQ!");
            obj.put("id", 42);
            obj.put("timeMillisSince1970", System.currentTimeMillis());
        } catch (JSONException e) {
            Log.e(C.TAG, "Erm not thought possible");
        }
    }

    Notification n = createNotification(settings, obj);

    if (n != null) {
        NotificationManager nm = (NotificationManager) mContext.getSystemService(Service.NOTIFICATION_SERVICE);
        nm.notify(NOTIFICATION_MASK | obj.optInt("id"), n);
    }
    mPrefs.edit().remove(key).commit();
}

From source file:fi.iki.murgo.irssinotifier.SettingsActivity.java

private void handleColorPicker() {
    Preference colorPickerPref = findPreference("PickCustomLightColor");
    colorPickerPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        @Override//from w  ww. ja  v  a  2s .c o m
        public boolean onPreferenceClick(final Preference preference) {
            final Context ctx = SettingsActivity.this;

            final Preferences preferences = new Preferences(ctx);
            final int color = preferences.getCustomLightColor();

            NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
            builder.setSmallIcon(R.drawable.notification_icon);
            builder.setTicker("Preview selected color");
            builder.setAutoCancel(false);
            builder.setOngoing(false);
            builder.setContentText("Wait for the screen to turn off to see selected light color in action");
            builder.setContentTitle("Preview light color");
            builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0));
            builder.setLights(color, 300, 5000);

            final Notification notification = builder.build();
            final NotificationManager notificationManager = (NotificationManager) ctx
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(666, notification);

            final AmbilWarnaDialog dialog = new AmbilWarnaDialog(ctx, color,
                    new AmbilWarnaDialog.OnAmbilWarnaListener() {
                        @Override
                        public void onCancel(AmbilWarnaDialog dialog) {
                            notificationManager.cancel(666);
                        }

                        @Override
                        public void onOk(AmbilWarnaDialog dialog, int color) {
                            notificationManager.cancel(666);
                            preferences.setCustomLightColor(color);
                        }

                        @Override
                        public void onColorChanged(AmbilWarnaDialog dialog, int color) {
                            notification.ledARGB = color;
                            notificationManager.notify(666, notification);
                        }
                    });

            dialog.show();

            return true;
        }
    });
}

From source file:com.example.jkgan.pmot.service.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//from  w  w  w  . j  a  va 2s .c o  m
 */
private void sendNotification(final String message, final String title, String id) {

    String url = MyApplication.getApiUrl() + "/promotions/" + id + "?token="
            + MyApplication.getUser().getToken();
    final Promotion[] promotion = { null };

    final OkHttpClient client = new OkHttpClient();

    final Request request = new Request.Builder().url(url).build();

    //        new Thread(new Runnable() {
    //            @Override
    //            public void run() {
    Response response = null;
    try {
        response = client.newCall(request).execute();
        JSONObject jsnObj2 = new JSONObject(response.body().string());

        promotion[0] = new Promotion(jsnObj2.optString("pName"), jsnObj2.optString("description"),
                jsnObj2.optString("id"),
                jsnObj2.getJSONObject("image").getJSONObject("medium").optString("url"),
                jsnObj2.getJSONObject("image").getJSONObject("small").optString("url"),
                jsnObj2.optString("term_and_condition"), jsnObj2.optString("name"),
                jsnObj2.optString("address"), jsnObj2.optString("sId"), getDate(jsnObj2.optString("starts_at")),
                getDate(jsnObj2.optString("expires_at")), jsnObj2.optString("phone"));

        Intent intent = new Intent(getApplicationContext(), PromotionActivity.class);
        intent.putExtra("NAME", promotion[0].getName());
        intent.putExtra("SHOP_ID", promotion[0].getId());
        intent.putExtra("IMAGE", promotion[0].getImage());
        intent.putExtra("DESCRIPTION", promotion[0].getDescription());
        intent.putExtra("TNC", promotion[0].getTnc());
        intent.putExtra("SHOP_NAME", promotion[0].getShop().getName());
        intent.putExtra("ADDRESS", promotion[0].getShop().getAddress());
        intent.putExtra("START", promotion[0].getStarts_at());
        intent.putExtra("EXPIRE", promotion[0].getExpires_at());
        intent.putExtra("PHONE", promotion[0].getShop().getPhone());
        intent.putExtra("SUBSCRIBED", true);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */,
                intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        if (numMessages >= 1) {
            notificationTitle += (", " + title);

            notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher).setContentTitle((numMessages + 1) + " new promotions")
                    .setContentText(notificationTitle).setAutoCancel(true).setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            numMessages = 0;

            notificationBuilder.setContentText(message).setNumber(++numMessages);

        } else {
            notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(message)
                    .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

            notificationTitle = title;
            numMessages++;
            //            notificationBuilder.setContentText(message)
            //                    .setNumber(++numMessages);
        }

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

        // On gnre un nombre alatoire pour pouvoir afficher plusieurs notifications
        notificationManager.notify(notifyID, notificationBuilder.build());
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.rocketsingh.biker.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//* ww w  .ja  v  a  2s. co  m*/
public static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    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, MapActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    // 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,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    System.out.println("notification====>" + message);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    // notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0x00000000;
    notification.ledOnMS = 0;
    notification.ledOffMS = 0;
    notificationManager.notify(AndyConstants.NOTIFICATION_ID, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "WakeLock");
    wakeLock.acquire();
    wakeLock.release();

}

From source file:com.careme.apvereda.careme.AccumulatorService.java

private void shownot(String s) {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_notifications);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.mipmap.ic_notsmall).setLargeIcon(bitmap).setContentTitle("Cuidado!")
            .setContentText(s).setVibrate(new long[] { 100, 250, 100, 500 });
    Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    int mNotificationId = 001;
    NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

From source file:com.belatrix.events.utils.fcm.EventsFirebaseMessagingService.java

private void sendNotification(String messageTitle, String messageBody) {
    Intent intent = MainActivity.makeIntent(this);
    intent.putExtra(MainActivity.PARAM_FROM_NOTIFICATION, true);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(messageTitle);
    bigTextStyle.bigText(messageBody);/*from w  w  w .  java 2 s  .  co  m*/

    Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.bx_connect_white).setLargeIcon(icon).setStyle(bigTextStyle)
            .setContentText(messageBody).setContentTitle(messageTitle).setAutoCancel(true)
            .setContentIntent(pendingIntent).setSound(alarmSound).setLights(0xFF8F0300, 1000, 200)
            .setPriority(Notification.PRIORITY_MAX);

    //for vibration
    Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(1000);

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

    long time = new Date().getTime();
    String tmpStr = String.valueOf(time);
    String last4Str = tmpStr.substring(tmpStr.length() - 5);
    int notificationId = Integer.valueOf(last4Str);

    notificationManager.notify(notificationId, notificationBuilder.build());
}

From source file:com.afrozaar.jazzfestreporting.ResumableUpload.java

public static void showSelectableNotification(String videoId, Context context) {
    Log.d(TAG, String.format("Posting selectable notification for video ID [%s]", videoId));
    final NotificationManager notifyManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    Intent notificationIntent = new Intent(context, PlayActivity.class);
    notificationIntent.putExtra(MainActivity.YOUTUBE_ID, videoId);
    notificationIntent.setAction(Intent.ACTION_VIEW);

    URL url;/* www . j  a v  a2 s  . c  om*/
    try {
        url = new URL("https://i1.ytimg.com/vi/" + videoId + "/mqdefault.jpg");
        Bitmap thumbnail = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentTitle(context.getString(R.string.watch_your_video))
                .setContentText(context.getString(R.string.see_the_newly_uploaded_video))
                .setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_stat_device_access_video)
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(thumbnail));
        notifyManager.notify(PLAYBACK_NOTIFICATION_ID, builder.build());
        Log.d(TAG, String.format("Selectable notification for video ID [%s] posted", videoId));
    } catch (MalformedURLException e) {
        Log.e(TAG, e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
}

From source file:com.android.settings.locationprivacy.LocationPrivacyDialog.java

@Override
protected void onPause() {
    System.out.println("onPause");
    if (needRestartActivity) {
        Intent intent = new Intent(this, LocationPrivacyDialog.class);
        intent.putExtra("app", app);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Notification noti = new Notification.Builder(this)
                .setContentTitle(getResources().getString(R.string.lp_notification_title))
                .setContentText(getResources().getString(R.string.lp_notification_))
                .setSmallIcon(R.drawable.ic_settings_locationprivacy).setContentIntent(pIntent)
                .setAutoCancel(true).build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, noti);

    }/*from   w w w  . ja  va 2s. c o m*/
    super.onPause();
}