NullPointerException
NullPointerException

Reputation: 37635

How to add flags to a notification (status bar notification)?

im following the tutorial from developers guide but i have a problem...

tutorial says: "To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object"

but... how i can add the flag to my notification?

notification doesn't have any kind of function to add flags.... then? how i can do it?

Upvotes: 4

Views: 8522

Answers (3)

Ogbuji Stanley
Ogbuji Stanley

Reputation: 1

Below is the code you can try, Its working.

public void notification(Context context,String title, String text) {
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(context)
        .setAutoCancel(true)
        .setSound(alarmSound)
        .setSmallIcon(R.drawable.appicon)
        .setContentTitle(title)
        .setContentText(text);

    Intent notificationIntent = new Intent(this,gps_get_data.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,                                           PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
}

Upvotes: 0

Gruntled
Gruntled

Reputation: 161

Notification flags is a public member field.

Notification notif = new Notification(R.drawable.icon, shortMsg,
                System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;

Upvotes: 16

ShadowGod
ShadowGod

Reputation: 7981

Flags go on the end when you define the Intent, like this:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, Notification.FLAG_AUTO_CANCEL);

Upvotes: 1

Related Questions