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:angeloid.dreamnarae.Boot_Script.java

@Override
public void onReceive(Context c, Intent i) {

    boot_top = c.getString(R.string.boot_top);
    boot_bottom = c.getString(R.string.boot_bottom);
    boot_ticker = c.getString(R.string.boot_tickker);

    if (!(RootTools.isAccessGiven())) {
        Toast.makeText(c, R.string.noroottoast, Toast.LENGTH_LONG).show();
    } else {/* w  w  w.ja v a2s  . c  o m*/
        if (new File("/system/etc/dreamnarae.sh").exists()) {
            CommandCapture command = new CommandCapture(0, "mount -o rw,remount /system",
                    "sh /system/etc/dreamnarae.sh", "sh /system/etc/install-recovery.sh");

            NotificationManager manager = (NotificationManager) c
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder ncbuilder = new NotificationCompat.Builder(c);
            ncbuilder.setContentTitle(boot_top);
            ncbuilder.setContentText(boot_bottom);
            ncbuilder.setSmallIcon(R.drawable.ic_launcher);
            ncbuilder.setAutoCancel(true);
            ncbuilder.setTicker(boot_ticker);
            Vibrator vibe = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
            try {
                RootTools.getShell(true).add(command).waitForFinish();
                Log.d("debug", "ok!");
                vibe.vibrate(200);
                manager.notify(1, ncbuilder.build());
            } catch (InterruptedException e) {
            } catch (IOException e) {
            } catch (TimeoutException e) {
            } catch (RootDeniedException e) {
            }

        }

    }
}

From source file:com.ithinkbest.taipeiok.NavigationDrawerActivity.java

private void notifyGooglePlay() {
    int idGooglePlay = 12345;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.to_google_play));
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ToGooglePlayActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ToGooglePlayActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(idGooglePlay, mBuilder.build());

}

From source file:com.ithinkbest.taipeiok.NavigationDrawerActivity.java

private void notifyAppWebpage() {
    int idGooglePlay = 12346;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.to_app_webpage));
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ToAppWebpageActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ToAppWebpageActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(idGooglePlay, mBuilder.build());

}

From source file:com.brq.wallet.activity.receive.ReceiveCoinsActivity.java

@Subscribe
public void syncStopped(SyncStopped event) {
    TextView tvRecv = (TextView) findViewById(R.id.tvReceived);
    TextView tvRecvWarning = (TextView) findViewById(R.id.tvReceivedWarningAmount);
    final WalletAccount selectedAccount = _mbwManager.getSelectedAccount();
    final List<TransactionSummary> transactionsSince = selectedAccount.getTransactionsSince(_receivingSince);
    final ArrayList<TransactionSummary> interesting = new ArrayList<TransactionSummary>();
    CurrencyValue sum = ExactBitcoinValue.ZERO;
    for (TransactionSummary item : transactionsSince) {
        if (item.toAddresses.contains(_address)) {
            interesting.add(item);/*from w  w w .  j  a  v  a  2s.c o m*/
            sum = item.value;
        }
    }

    if (interesting.size() > 0) {
        tvRecv.setText(getString(R.string.incoming_payment)
                + Utils.getFormattedValueWithUnit(sum, _mbwManager.getBitcoinDenomination()));
        // if the user specified an amount, also check it if it matches up...
        if (!CurrencyValue.isNullOrZero(_amount)) {
            tvRecvWarning.setVisibility(sum.equals(_amount) ? View.GONE : View.VISIBLE);
        } else {
            tvRecvWarning.setVisibility(View.GONE);
        }
        tvRecv.setVisibility(View.VISIBLE);
        if (!sum.equals(_lastAddressBalance)) {
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setSound(soundUri, AudioManager.STREAM_NOTIFICATION); //This sets the sound to play
            notificationManager.notify(0, mBuilder.build());

            _lastAddressBalance = sum;
        }
    } else {
        tvRecv.setVisibility(View.GONE);
    }
}

From source file:com.katamaditya.apps.weather4u.weathersync.Weather4USyncAdapter.java

public void notifyWeather() {
    Context context = getContext();
    //checking the last update and notify if it' the first of the day
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String displayNotificationsKey = context.getString(R.string.pref_enable_notifications_key);
    boolean displayNotifications = prefs.getBoolean(displayNotificationsKey,
            Boolean.parseBoolean(context.getString(R.string.pref_enable_notifications_default)));

    if (displayNotifications) {

        String lastNotificationKey = context.getString(R.string.pref_last_notification);
        long lastSync = prefs.getLong(lastNotificationKey, 0);

        if (System.currentTimeMillis() - lastSync >= getNotificationTimeGap()) {
            // Last sync was more than 1 day ago, let's send a notification with the weather.
            String locationQuery = WeatherUtil.getPreferredLocation(context);

            Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationQuery,
                    System.currentTimeMillis());

            // we'll query our contentProvider, as always
            Cursor cursor = context.getContentResolver().query(weatherUri, NOTIFY_WEATHER_PROJECTION, null,
                    null, null);//  w  w  w . ja  v  a2s  .c  o m

            if (cursor.moveToFirst()) {
                int weatherId = cursor.getInt(INDEX_WEATHER_ID);
                double high = cursor.getDouble(INDEX_MAX_TEMP);
                double low = cursor.getDouble(INDEX_MIN_TEMP);
                String desc = cursor.getString(INDEX_SHORT_DESC);

                int iconId = WeatherUtil.getIconResourceForWeatherCondition(weatherId);
                Resources resources = context.getResources();
                Bitmap largeIcon = BitmapFactory.decodeResource(resources,
                        WeatherUtil.getArtResourceForWeatherCondition(weatherId));
                String title = getTitle();

                // Define the text of the forecast.
                String contentText = String.format(context.getString(R.string.format_notification), desc,
                        WeatherUtil.formatTemperature(context, high),
                        WeatherUtil.formatTemperature(context, low));

                // NotificationCompatBuilder is a very convenient way to build backward-compatible
                // notifications.  Just throw in some data.
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getContext())
                        .setColor(resources.getColor(R.color.weather4u_light_blue)).setSmallIcon(iconId)
                        .setLargeIcon(largeIcon).setContentTitle(title).setContentText(contentText);
                mBuilder.setAutoCancel(true);

                // Make something interesting happen when the user clicks on the notification.
                // In this case, opening the app is sufficient.
                Intent resultIntent = new Intent(context, MainActivity.class);

                // The stack builder object will contain an artificial back stack for the
                // started Activity.
                // This ensures that navigating backward from the Activity leads out of
                // your application to the Home screen.
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);

                NotificationManager mNotificationManager = (NotificationManager) getContext()
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                // WEATHER_NOTIFICATION_ID allows you to update the notification later on.
                mNotificationManager.notify(WEATHER_NOTIFICATION_ID, mBuilder.build());

                //refreshing last sync
                SharedPreferences.Editor editor = prefs.edit();
                editor.putLong(lastNotificationKey, System.currentTimeMillis());
                editor.commit();
            }
            cursor.close();
        }
    }
}

From source file:com.ntsync.android.sync.syncadapter.SyncAdapter.java

private void notifyUserPhotoNotSynced(String accountName) {
    Account account = new Account(accountName, Constants.ACCOUNT_TYPE);
    AccountManager acm = AccountManager.get(mContext);
    String synced = acm.getUserData(account, NOTIF_SHOWN_PHOTO_SYNCED);
    if (synced == null) {
        Intent shopIntent = new Intent(mContext, ShopActivity.class);
        shopIntent.putExtra(ShopActivity.PARM_ACCOUNT_NAME, accountName);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
        stackBuilder.addParentStack(ShopActivity.class);
        stackBuilder.addNextIntent(shopIntent);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(Constants.NOTIF_ICON)
                .setContentTitle(getText(R.string.notif_photonotsynced_title)).setContentText(accountName)
                .setAutoCancel(true)//from ww w .  java  2 s. c  om
                .setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT))
                .setOnlyAlertOnce(true);
        NotificationManager mNotificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(Constants.NOTIF_PHOTO_NOT_SYNCED, mBuilder.build());
        acm.setUserData(account, NOTIF_SHOWN_PHOTO_SYNCED, String.valueOf(System.currentTimeMillis()));
    }
}

From source file:com.athena.broncobattle.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments

    Log.i("selectItem", "New item selected: " + position);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // Sets an ID for the notification, so it can be updated
    int notifyID = 1;
    Builder mNotifyBuilder = new NotificationCompat.Builder(this).setContentTitle("New Message")
            .setContentText("You've received new messages.").setSmallIcon(android.R.drawable.ic_menu_compass);

    mNotifyBuilder.setContentText("Some text!").setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated./* ww  w .j  a v  a2  s  .c om*/
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());

    if (!(position < 0)) {
        if (fragments[position] == null) {
            switch (position) {
            case 0:
                fragments[position] = new QuestionsFragment();
                break;
            case 1:
                fragments[position] = new LeaderboardFragment();
                break;
            case 2:
                fragments[position] = new StatsFragment();
                break;
            }
        }

        // Fragment fragment = new PlanetFragment();
        // Bundle args = new Bundle();
        // args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        // fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragments[position]).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position + 1, true);
        setTitle(mDrawerButtonTitles[position]);
        // setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
}

From source file:com.ntsync.android.sync.syncadapter.SyncAdapter.java

private void notifyUserConctactNotSynced(int maxCount, int totalLocalContacts, String accountName) {
    AccountManager acm = AccountManager.get(mContext);
    Account account = new Account(accountName, Constants.ACCOUNT_TYPE);
    String lastTimeShown = acm.getUserData(account, NOTIF_SHOWN_CONTACTS_SYNCED);
    Long lastTime;/*  w  w  w . j a  va  2  s .  co m*/
    try {
        lastTime = lastTimeShown != null ? Long.parseLong(lastTimeShown) : null;
    } catch (NumberFormatException ex) {
        LogHelper.logWCause(TAG,
                "Invalid Config-Settings:" + NOTIF_SHOWN_CONTACTS_SYNCED + " Value:" + lastTimeShown, ex);
        lastTime = null;
    }

    if (lastTime == null || System.currentTimeMillis() > lastTime.longValue() + NOTIF_WAIT_TIME) {
        // Create Shop-Intent
        Intent shopIntent = new Intent(mContext, ShopActivity.class);
        shopIntent.putExtra(ShopActivity.PARM_ACCOUNT_NAME, account.name);
        // Adds the back stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
        stackBuilder.addParentStack(ShopActivity.class);
        stackBuilder.addNextIntent(shopIntent);

        // Create Notification
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(Constants.NOTIF_ICON)
                .setContentTitle(String.format(getText(R.string.notif_contactnotsynced_title), maxCount,
                        totalLocalContacts))
                .setContentText(String.format(getText(R.string.notif_contactnotsynced_content), account.name))
                .setAutoCancel(true).setOnlyAlertOnce(true)
                .setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
        NotificationManager mNotificationManager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(Constants.NOTIF_CONTACTS_NOT_SYNCED, mBuilder.build());
        acm.setUserData(account, NOTIF_SHOWN_CONTACTS_SYNCED, String.valueOf(System.currentTimeMillis()));
    }
}

From source file:android.romstats.ReportingService.java

private void promptUser() {
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Intent mainActivity = new Intent(getApplicationContext(), AnonymousStats.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mainActivity, 0);

    Notification notification = new NotificationCompat.Builder(getBaseContext())
            .setSmallIcon(R.drawable.ic_launcher).setTicker(getString(R.string.notification_ticker))
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(getString(R.string.notification_desc)).setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent).setAutoCancel(true).build();

    nm.notify(Utilities.NOTIFICATION_ID, notification);
}

From source file:com.granita.contacticloudsync.syncadapter.AccountSettings.java

@TargetApi(21)
protected void showNotification(int id, String title, String message) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder n = new Notification.Builder(context);
    if (Build.VERSION.SDK_INT >= 16) {
        n.setPriority(Notification.PRIORITY_HIGH);
        n.setStyle(new Notification.BigTextStyle().bigText(message));
    }//from w  w w  .java  2  s .c o m
    if (Build.VERSION.SDK_INT >= 20)
        n.setLocalOnly(true);
    if (Build.VERSION.SDK_INT >= 21)
        n.setCategory(Notification.CATEGORY_SYSTEM);
    n.setSmallIcon(R.drawable.ic_launcher);
    n.setContentTitle(title);
    n.setContentText(message);
    nm.notify(id, Build.VERSION.SDK_INT >= 16 ? n.build() : n.getNotification());
}