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(String tag, int id, Notification notification) 

Source Link

Document

Post a notification to be shown in the status bar.

Usage

From source file:com.cloverstudio.spika.GCMIntentService.java

@SuppressWarnings("deprecation")
public void triggerNotification(Context context, String message, String fromName, Bundle pushExtras) {

    if (fromName != null) {
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);//from   w w  w. jav a2 s  .c o  m
        Notification notification = new Notification(R.drawable.icon_notification, message,
                System.currentTimeMillis());
        notification.number = mNotificationCounter + 1;
        mNotificationCounter = mNotificationCounter + 1;

        Intent intent = new Intent(this, SplashScreenActivity.class);
        intent.replaceExtras(pushExtras);
        intent.putExtra(Const.PUSH_INTENT, true);
        intent.setAction(Long.toString(System.currentTimeMillis()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_FROM_BACKGROUND
                | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, notification.number, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this, context.getString(R.string.app_name), message, pendingIntent);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        String notificationId = Double.toString(Math.random());
        notificationManager.notify(notificationId, 0, notification);
    }
}

From source file:com.snappy.GCMIntentService.java

@SuppressWarnings("deprecation")
public void triggerNotification(Context context, String message, String fromName, Bundle pushExtras,
        String messageData) {/*from w  w  w  .j  av a  2s.co  m*/

    if (fromName != null) {
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);

        Notification notification = new Notification(R.drawable.icon_notification, message,
                System.currentTimeMillis());

        notification.number = mNotificationCounter + 1;
        mNotificationCounter = mNotificationCounter + 1;

        Intent intent = new Intent(this, SplashScreenActivity.class);
        intent.replaceExtras(pushExtras);
        intent.putExtra(Const.PUSH_INTENT, true);
        intent.setAction(Long.toString(System.currentTimeMillis()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_FROM_BACKGROUND
                | Intent.FLAG_ACTIVITY_TASK_ON_HOME);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, notification.number, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setLatestEventInfo(this, context.getString(R.string.app_name), messageData, pendingIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        String notificationId = Double.toString(Math.random());
        notificationManager.notify(notificationId, 0, notification);

    }

    Log.i(LOG_TAG, message);
    Log.i(LOG_TAG, fromName);

}

From source file:com.amazon.cordova.plugin.ADMMessageHandler.java

/**
 * Creates a notification when app is not running or is not in foreground. It puts the message info into the Intent
 * extra//from  www .  ja  va  2 s.c o  m
 * 
 * @param context
 * @param extras
 */
public void createNotification(Context context, Bundle extras) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    // reuse the intent so that we can combine multiple messages into extra
    if (notificationIntent == null) {
        notificationIntent = new Intent(this, ADMHandlerActivity.class);
    }
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final Builder notificationBuilder = new Notification.Builder(context);
    notificationBuilder.setSmallIcon(context.getApplicationInfo().icon).setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent);

    if (this.shouldShowMessageInNotification()) {
        String message = extras.getString(PushPlugin.MESSAGE);
        notificationBuilder.setContentText(Html.fromHtml(message).toString());
    } else {
        notificationBuilder.setContentText(this.defaultMessageTextInNotification());
    }

    String title = appName;
    notificationBuilder.setContentTitle(title).setTicker(title);
    notificationBuilder.setAutoCancel(true);
    // Because the ID remains unchanged, the existing notification is updated.
    notificationManager.notify((String) appName, NOTIFICATION_ID, notificationBuilder.getNotification());
}

From source file:com.android.madpausa.cardnotificationviewer.ConcreteNotificationListenerService.java

/**
 * sends the service notification/*from  w  ww  . jav  a 2  s . c  o  m*/
 */
private void handleServiceNotification() {

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

    //filtering archived notification list
    List<StatusBarNotification> filteredArchivedNotificationList = baseNotificationFilter
            .applyFilter(archivedNotificationMap.values(), notificationGroups, true);
    int filteredArchiveddSize = filteredArchivedNotificationList.size();

    //should show notification only if there are notifications to be shown
    if (filteredArchiveddSize > 0) {
        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);

        nBuilder.setContentTitle(
                String.format(getString(R.string.service_notification_text), filteredArchiveddSize));

        nBuilder.setSmallIcon(R.drawable.ic_notification);
        //gets the correct color resource, based on android version
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            nBuilder.setColor(getResources().getColor(R.color.app_background, null));
        else //noinspection deprecation
            nBuilder.setColor(getResources().getColor(R.color.app_background));

        //setting the intent
        Intent resultIntent = new Intent(this, MainActivity.class);

        //setting the extra containing the archived notifications
        resultIntent.putExtra(ARCHIVED_NOTIFICATIONS_EXTRA, new HashSet<>(archivedNotificationMap.keySet()));

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        nBuilder.setContentIntent(resultPendingIntent);

        //low priority, not min, as it has to show in the lockscreen
        nBuilder.setPriority(Notification.PRIORITY_LOW);

        Notification notification = nBuilder.build();

        //this notification should be sticky
        notification.flags |= Notification.FLAG_NO_CLEAR;
        nManager.notify(SERVICE_NOTIFICATION, 0, notification);
    }
    //else I should remove the notification
    else
        nManager.cancel(SERVICE_NOTIFICATION, 0);
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public Object notifyStatusBar(String tickerText, String contentTitle, String contentBody, boolean vibrate,
        boolean flashLights, Hashtable args) {
    int id = getContext().getResources().getIdentifier("icon", "drawable",
            getContext().getApplicationInfo().packageName);

    NotificationManager notificationManager = (NotificationManager) getContext()
            .getSystemService(Activity.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent();
    notificationIntent.setComponent(getActivity().getComponentName());
    PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, notificationIntent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext())
            .setContentIntent(contentIntent).setSmallIcon(id).setContentTitle(contentTitle)
            .setTicker(tickerText);/*from  w w  w. ja v  a 2  s .com*/
    if (flashLights) {
        builder.setLights(0, 1000, 1000);
    }
    if (vibrate) {
        builder.setVibrate(new long[] { 0, 100, 1000 });
    }
    if (args != null) {
        Boolean b = (Boolean) args.get("persist");
        if (b != null && b.booleanValue()) {
            builder.setAutoCancel(false);
            builder.setOngoing(true);
        } else {
            builder.setAutoCancel(false);
        }
    } else {
        builder.setAutoCancel(true);
    }
    Notification notification = builder.build();
    int notifyId = 10001;
    notificationManager.notify("CN1", notifyId, notification);
    return new Integer(notifyId);
}