Example usage for android.app Notification FLAG_AUTO_CANCEL

List of usage examples for android.app Notification FLAG_AUTO_CANCEL

Introduction

In this page you can find the example usage for android.app Notification FLAG_AUTO_CANCEL.

Prototype

int FLAG_AUTO_CANCEL

To view the source code for android.app Notification FLAG_AUTO_CANCEL.

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if the notification should be canceled when it is clicked by the user.

Usage

From source file:org.rti.rcd.ict.lgug.C2DMReceiver.java

@Override
protected void onMessage(Context context, Intent intent) {
    //String accountName = intent.getExtras().getString(Config.C2DM_ACCOUNT_EXTRA);
    String accountName = intent.getStringExtra("account");
    String message = intent.getExtras().getString(Config.C2DM_MESSAGE_EXTRA);
    Log.d(TAG, "Messaging request received for account " + accountName);
    //        CoconutActivity c = CoconutActivity.getRef();
    //        c.displayMessage( message );
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.icon;
    CharSequence tickerText = "Olutindo";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;

    notification.flags = Notification.FLAG_AUTO_CANCEL;

    notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0xff00ff00;/*from  ww w. j a  v  a 2 s .  c o  m*/
    notification.ledOnMS = 300;
    notification.ledOffMS = 1000;

    //Context context = getApplicationContext();
    Log.d(TAG, "Triggering once_off replication upon receipt of notification: " + message);
    Properties properties = new Properties();
    try {
        InputStream rawResource = getResources().openRawResource(R.raw.coconut);
        properties.load(rawResource);
        System.out.println("The properties are now loaded");
        System.out.println("properties: " + properties);
    } catch (Resources.NotFoundException e) {
        System.err.println("Did not find raw resource: " + e);
    } catch (IOException e) {
        System.err.println("Failed to open microlog property file");
    }
    String localDb = "http://localhost:" + properties.getProperty("local_couch_app_port") + "/"
            + properties.getProperty("app_db");
    Log.d(TAG, "localDb: " + localDb);

    //       String localReplicationDbUrl = "http://localhost:" + properties.getProperty("local_couch_app_port") +"/_replicate";
    //       String replicationMasterUrl = "http://" + properties.getProperty("master_server") + "/coconut";
    //       String replicationDataFromMaster = "{\"_id\": \"once_off_from_master\",\"target\":\"" + localDb + "\",\"source\":\"" + replicationMasterUrl + "\"}";
    //       String replicationDataToMaster = "{\"_id\": \"once_off_to_master\",\"target\":\"" + replicationMasterUrl + "\",\"source\":\"" + localDb + "\"}";
    //    
    //       try {
    //         HTTPRequest.post(localReplicationDbUrl, replicationDataFromMaster);
    //      } catch (JSONException e) {
    //         Log.d(TAG, "Problem installing replication target FromMaster. replicationMasterUrl: " + replicationMasterUrl + " Error:" + e.getMessage());
    //         e.printStackTrace();
    //      } catch (ConnectException e) {
    //         Log.d(TAG, "Unable to connect to replicationMasterUrl: " + replicationMasterUrl + " Error:" + e.getMessage());
    //      }
    //       try {
    //          HTTPRequest.post(localReplicationDbUrl, replicationDataToMaster);
    //       } catch (JSONException e) {
    //          Log.d(TAG, "Problem installing replication target ToMaster. replicationMasterUrl: " + replicationMasterUrl + " Error:" + e.getMessage());
    //          e.printStackTrace();
    //       } catch (ConnectException e) {
    //          Log.d(TAG, "Unable to connect to replicationMasterUrl: " + replicationMasterUrl + " Error:" + e.getMessage());
    //      }
    CharSequence contentTitle = "New Olutindo Message";
    //CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, CoconutActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(context, contentTitle, message, contentIntent);

    mNotificationManager.notify(HELLO_ID, notification);
}

From source file:no.ntnu.wifimanager.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*w w  w .  j  a va2 s  .  c  om*/
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_stat_gcm;
    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, ClientListActivity.class);
    // 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, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:de.wikilab.android.friendica01.FileUploadService.java

private void showSuccessMsg(Context ctx) {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    //Instantiate the Notification:
    int icon = R.drawable.arrow_up;
    CharSequence tickerText = "Upload succeeded!";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    /*/* w ww.jav  a  2  s  .co m*/
    //TODO!!!
    //Define the Notification's expanded message and Intent:
    Context context = getApplicationContext();
    CharSequence contentTitle = "Upload successful, click to show details!";
    CharSequence contentText = targetFilename;
    Intent notificationIntent = new Intent(this, FilePreview.class);
    Bundle b = new Bundle();
    b.putInt("cid", clipId);
    notificationIntent.putExtras(b);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            
            
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
     */

    //Define the Notification's expanded message and Intent:
    Context context = getApplicationContext();
    PendingIntent nullIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    notification.setLatestEventInfo(context, tickerText, "Click to dismiss", nullIntent);

    //Pass the Notification to the NotificationManager:

    mNotificationManager.notify(UPLOAD_SUCCESS_ID, notification);
    //Toast.makeText(ctx, "Upload failed, please retry:\n" + txt, Toast.LENGTH_LONG).show();
}

From source file:il.ac.shenkar.todos.notifications.NotificationService.java

@Override
public void onHandleIntent(Intent intent) {
    taskListModel = TaskList.getSingletonObject(this);
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    //vibrator.vibrate(vibrationTime);
    vibrator.vibrate(vibrationTime);/*w  w w .  ja v  a 2 s.c  om*/
    taskListModel.getDataBase().open();

    String[] tokens = intent.getStringExtra("description").split(",");
    taskId = Integer.parseInt(tokens[0]);
    String title = tokens[1];
    String windowTitle = null;
    String toSend = null;
    Task triggeredTask = null;
    boolean alarmRepeating = true;

    // check if alert was triggered by the location alert
    if (title.equals("GPS location nearby !")) {
        alarmType = "GPS";
        alarmRepeating = false;
        taskId = intent.getIntExtra("Id", 0);
        windowTitle = "GPS location nearby !";
        toSend = "Title: " + tokens[3] + "\n\nLocation: " + tokens[2];
        triggeredTask = Utils.findTaskById(this, taskId);

        if (triggeredTask != null) {
            // remove the alarm
            new TaskAlarms(this).cancelProximityAlert(taskId, triggeredTask.getTaskTitle());
            taskListModel.getDataBase().disableTaskLocationAlert(taskId);
        }
    } else // triggered by time alert
    {
        alarmType = "Time";
        windowTitle = "Notification";
        toSend = "Title: " + title + "\n\nDescription: " + tokens[2];
        triggeredTask = Utils.findTaskById(this, taskId);

        if (triggeredTask != null) {
            String taskNotification = triggeredTask.getDate();
            // checks if the alarm has an interval
            if (!taskNotification.contains("(")) {
                alarmRepeating = false;
                new TaskAlarms(this).cancelAlarm(taskId, triggeredTask.getTaskTitle());
                taskListModel.getDataBase().disableTaskNotification(taskId);
            }
        }
    }

    // Prepare intent which is triggered if the
    // notification is selected
    Intent myIntent = new Intent(this, NotificationReceiverActivity.class);
    myIntent.putExtra("windowTitle", windowTitle);
    myIntent.putExtra("taskContent", toSend);
    PendingIntent pIntent = PendingIntent.getActivity(this, taskId, myIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Build notification
    Notification noti = new Notification.Builder(this).setContentTitle(title).setContentText(tokens[2])
            .setSmallIcon(R.drawable.app_icon).setContentIntent(pIntent).build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;
    noti.flags |= Notification.FLAG_SHOW_LIGHTS;
    noti.ledARGB = 0xff00ffff;
    noti.ledOnMS = 300;
    noti.ledOffMS = 1000;

    if (Utils.IS_DEFAULT_SOUND) {
        //To play the default sound with your notification:
        noti.defaults |= Notification.DEFAULT_SOUND;
    } else {
        new MediaPlayerHandler(this).playAudio(Utils.LOUD_NOTIFICATION__SOUND);
    }
    notificationManager.notify(taskId, noti);

    if (!alarmRepeating) {
        // brodcast a message to main activity
        messageToActivity();
    }
}

From source file:com.bakhtiyor.android.tumblr.TumblrService.java

private void showResultNotification(String message) {
    long when = System.currentTimeMillis();
    Notification notification = new Notification(R.drawable.icon, APP_TITLE, when);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    Intent notificationIntent = new Intent(this, TumblrService.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(getApplicationContext(), APP_TITLE, message, contentIntent);
    notificationManager.notify(RESULT_NOTIFICATION_ID, notification);
}

From source file:com.unlockdisk.android.opengl.MainGLActivity.java

public void generateNotification(int id, String notificationTitle, String notificationMessage) {
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "A New Message!",
            System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, MainGLActivity.class);
    // Intent notificationIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.android.com"));

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.setLatestEventInfo(MainGLActivity.this, notificationTitle, notificationMessage, pendingIntent);

    //Setting Notification Flags
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.DEFAULT_SOUND;
    //Adding the Custom Sound
    notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
    String uri = "android.resource://org.openmobster.notify.android.app/"
            + MainGLActivity.this.findSoundId(MainGLActivity.this, "beep");
    notification.sound = Uri.parse(uri);
    //  notification.sound = Uri.fromFile(new File(R.raw.a));
    notificationManager.notify(id, notification);
}

From source file:com.tcnr14.example.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///from w  ww  .  j a  v a  2s  .c  om
private 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, MainActivity.class);
    // 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, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateNotification(Context context, String message) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    notificationIntent.putExtra("alert", message);
    message = message + " would like to add you as friend";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();/*from ww w  . j  a v a2 s  .  c  om*/

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:com.oldsneerjaw.sleeptimer.CountdownNotifierTest.java

public void testPostNotification() {
    Date countdownEnds = new Date(0);

    notifier.postNotification(countdownEnds);

    Mockito.verify(mockCountdownTimeFormatFactory).getTimeFormat();
    Mockito.verify(mockNotificationManager).notify(Mockito.eq(NOTIFICATION_ID),
            Mockito.argThat(new BaseMatcher<Notification>() {
                @Override/*from  w  ww .j av a  2  s.  co m*/
                public boolean matches(Object o) {
                    if (!(o instanceof Notification)) {
                        return false;
                    }

                    Notification candidate = (Notification) o;

                    return TextUtils.equals(NOTIFICATION_TITLE, candidate.tickerText)
                            && (candidate.icon == R.drawable.ic_launcher)
                            && (candidate.priority == NotificationCompat.PRIORITY_DEFAULT)
                            && (candidate.contentIntent != null)
                            && ((candidate.flags & Notification.FLAG_ONGOING_EVENT) != 0)
                            && ((candidate.flags & Notification.FLAG_AUTO_CANCEL) == 0);
                }

                @Override
                public void describeTo(Description description) {
                    // Describe the notification that was expected in the event of test failure
                    description.appendText(
                            "a notification with the correct title, icon, default priority, a pending intent, set to ongoing and NOT auto cancel");
                }
            }));
}

From source file:vcc.mobile.pega.gcm.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//* ww w  .ja v a  2s.c  om*/
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String... message) {
    String content = message[0];
    String source = message[1];
    String id = message[2];
    int idInt = 0;
    try {
        idInt = Integer.valueOf(id);
    } catch (Exception e) {
        e.printStackTrace();
    }

    int icon = R.drawable.icon_launcher;
    long when = System.currentTimeMillis();
    String title = context.getString(R.string.app_name);

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, content, when);

    Intent notificationIntent = new Intent(context, NotificationActivity.class);
    // notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    // Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra(NotificationActivity.PARAM_SOURCE, source);
    notificationIntent.putExtra(NotificationActivity.PARAM_ID, id);

    PendingIntent intent = PendingIntent.getActivity(context, idInt, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, content, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(idInt, notification);
}