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.andrewchelladurai.simplebible.utilities.NotificationDisplayer.java

@Override
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "onReceive: called");
    long when = System.currentTimeMillis();
    int MID = (int) when;
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notIntent = new Intent(context, SimpleBibleActivity.class);
    notIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_notification_small_icon)
            .setContentTitle(context.getString(R.string.application_name))
            .setContentText(context.getString(R.string.notification_text)).setSound(alarmSound)
            .setAutoCancel(true).setWhen(when).setContentIntent(pendingIntent);

    notificationManager.notify(MID, builder.build());
    Log.d(TAG, "onReceive: reminder Created");
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncTask.java

protected Object work(Context context, DatabaseAdapter dba, String... params) throws ImportExportException {

    AccountManager accountManager = AccountManager.get(context);
    android.accounts.Account[] accounts = accountManager.getAccountsByType("com.google");

    String accountName = MyPreferences.getFlowzrAccount(context);
    if (accountName == null) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, FlowzrSyncActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        Builder mNotifyBuilder = new NotificationCompat.Builder(context);
        mNotifyBuilder.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentTitle(context.getString(R.string.flowzr_sync))
                .setContentText(context.getString(R.string.flowzr_choose_account));
        nm.notify(0, mNotifyBuilder.build());
        Log.i("Financisto", "account name is null");
        throw new ImportExportException(R.string.flowzr_choose_account);
    }/*from  w w w .  j  a  va 2 s  .co m*/
    Account useCredential = null;
    for (int i = 0; i < accounts.length; i++) {
        if (accountName.equals(((android.accounts.Account) accounts[i]).name)) {
            useCredential = accounts[i];
        }
    }
    accountManager.getAuthToken(useCredential, "ah", false, new GetAuthTokenCallback(), null);
    return null;
}

From source file:com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClient.java

private boolean displayNotification(final Bundle pushBundle, final Class<?> targetClass, String imageUrl,
        String iconImageUrl, String iconSmallImageUrl, Map<String, String> campaignAttributes,
        String intentAction) {/*from w  w  w  .  j  a  v  a2 s .  c  o  m*/
    log.info("Display Notification: " + pushBundle.toString());

    final String title = pushBundle.getString(NOTIFICATION_TITLE_PUSH_KEY);
    final String message = pushBundle.getString(NOTIFICATION_BODY_PUSH_KEY);

    final String campaignId = campaignAttributes.get(CAMPAIGN_ID_ATTRIBUTE_KEY);
    final String activityId = campaignAttributes.get(CAMPAIGN_ACTIVITY_ID_ATTRIBUTE_KEY);

    final int requestID = (campaignId + ":" + activityId + ":" + System.currentTimeMillis()).hashCode();

    final int iconResId = getNotificationIconResourceId(pushBundle.getString(NOTIFICATION_ICON_PUSH_KEY));
    if (iconResId == 0) {
        return false;
    }

    final Notification notification = createNotification(iconResId, title, message, imageUrl, iconImageUrl,
            iconSmallImageUrl,
            this.createOpenAppPendingIntent(pushBundle, targetClass, campaignId, requestID, intentAction));

    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

    if (android.os.Build.VERSION.SDK_INT >= ANDROID_LOLLIPOP) {
        log.info("SDK greater than 21 detected: " + android.os.Build.VERSION.SDK_INT);

        final String colorString = pushBundle.getString(NOTIFICATION_COLOR_PUSH_KEY);
        if (colorString != null) {
            int color;
            try {
                color = Color.parseColor(colorString);
            } catch (final IllegalArgumentException ex) {
                log.warn("Couldn't parse campaign notification color.", ex);
                color = 0;
            }
            Exception exception = null;
            try {
                final Field colorField = notification.getClass().getDeclaredField("color");
                colorField.setAccessible(true);
                colorField.set(notification, color);
            } catch (final IllegalAccessException ex) {
                exception = ex;
            } catch (final NoSuchFieldException ex) {
                exception = ex;
            }
            if (exception != null) {
                log.error("Couldn't set campaign notification color : " + exception.getMessage(), exception);
            }
        }
    }

    final NotificationManager notificationManager = (NotificationManager) pinpointContext
            .getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(requestID, notification);
    return true;
}

From source file:com.mutu.gpstracker.streaming.CustomUpload.java

private void notifyError(Context context, Exception e) {
    Log.e(TAG, "Custom upload failed", e);
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

    int icon = R.drawable.ic_maps_indicator_current_position;
    CharSequence tickerText = context.getText(R.string.customupload_failed);
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);

    Context appContext = context.getApplicationContext();
    CharSequence contentTitle = tickerText;
    CharSequence contentText = e.getMessage();
    Intent notificationIntent = new Intent(context, CustomUpload.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(appContext, contentTitle, contentText, contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

From source file:at.ac.uniklu.mobile.sportal.service.MutingService.java

private void notifyUser(int id, boolean ongoing, String tickerText, String contentTitle, String contentText,
        int icon, Intent intent) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(this).setSmallIcon(icon).setTicker(tickerText)
            .setContentText(contentText).setContentTitle(contentTitle).setContentIntent(contentIntent)
            .setWhen(System.currentTimeMillis()).setOngoing(ongoing).build();

    notificationManager.notify(id, notification);
}

From source file:com.androzic.location.LocationService.java

private void updateNotification() {
    if (locationManager != null) {
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, getNotification());
    }/*from ww  w .j  a v a2s  . c om*/
}

From source file:com.raceyourself.android.samsung.ProviderService.java

private void enableIcon() {
    if (iconEnabled)
        return;/*  w  ww .j  a  v  a2  s  . c o  m*/
    iconEnabled = true;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.rylogo)
            .setContentTitle(getString(R.string.notification_title))
            .setContentText(getString(R.string.notification_message));

    Intent intent = new Intent(this, PopupActivity.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
    mNotifyMgr.notify(TETHER_NOTIFICATION_ID, mBuilder.build());
}

From source file:com.android.mms.transaction.MessagingNotification.java

private static void notifyFailed(Context context, boolean isDownload, long threadId, boolean noisy) {
    // TODO factor out common code for creating notifications
    boolean enabled = MessagingPreferenceActivity.getNotificationEnabled(context);
    if (!enabled) {
        return;/*from  www  .  j  a v  a 2  s.c  om*/
    }

    // Strategy:
    // a. If there is a single failure notification, tapping on the notification goes
    //    to the compose view.
    // b. If there are two failure it stays in the thread view. Selecting one undelivered
    //    thread will dismiss one undelivered notification but will still display the
    //    notification.If you select the 2nd undelivered one it will dismiss the notification.
    int totalFailedCount = getUndeliveredMessageCount(context);

    Intent failedIntent;
    Notification notification = new Notification();
    String title;
    String description;
    if (totalFailedCount > 1) {
        description = context.getString(R.string.notification_failed_multiple,
                Integer.toString(totalFailedCount));
        title = context.getString(R.string.notification_failed_multiple_title);
    } else {
        title = isDownload ? context.getString(R.string.message_download_failed_title)
                : context.getString(R.string.message_send_failed_title);

        description = context.getString(R.string.message_failed_body);
    }

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    // Get failed intent by folder mode or conversation mode.
    if (MessageUtils.isMailboxMode()) {
        failedIntent = getFailedIntentFromFolderMode(context, totalFailedCount, isDownload);
        if (failedIntent == null) {
            return;
        } else if (isDownload) {
            // When isDownload is true, the valid threadId is passed into this function.
            failedIntent.putExtra(FAILED_DOWNLOAD_FLAG, true);
        } else {
            failedIntent.putExtra(UNDELIVERED_FLAG, true);
        }
    } else {
        failedIntent = getFailedIntentFromConversationMode(context, isDownload, threadId);
    }

    taskStackBuilder.addNextIntent(failedIntent);

    notification.icon = R.drawable.stat_notify_sms_failed;

    notification.tickerText = title;

    notification.setLatestEventInfo(context, title, description,
            taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));

    if (noisy) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        boolean vibrate = sp.getBoolean(MessagingPreferenceActivity.NOTIFICATION_VIBRATE,
                false /* don't vibrate by default */);
        if (vibrate) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }

        String ringtoneStr = sp.getString(MessagingPreferenceActivity.NOTIFICATION_RINGTONE, null);
        notification.sound = TextUtils.isEmpty(ringtoneStr) ? null : Uri.parse(ringtoneStr);
    }

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

    if (isDownload) {
        notificationMgr.notify(DOWNLOAD_FAILED_NOTIFICATION_ID, notification);
    } else {
        notificationMgr.notify(MESSAGE_FAILED_NOTIFICATION_ID, notification);
    }
}