29

I have the following code which I am using for an android app:

package com.authorwjf.e_notifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.avatar), 
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height), 
                true);
        Intent intent = new Intent(this, Main.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 01, intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setContentTitle("This is the title");
        builder.setContentText("This is the text");
        builder.setSubText("Some sub text");
        builder.setNumber(101);
        builder.setContentIntent(pendingIntent);
        builder.setTicker("Fancy Notification");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setLargeIcon(bm);
        builder.setAutoCancel(true);
        builder.setPriority(0);
        Notification notification = builder.build();
        NotificationManager notificationManger = 
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManger.notify(01, notification);        
    }

}

Basically the app when launched creates a notification, now I have a few questions:

  • Is it possible when the app is launched that the notification is displayed in the the pull down notification list but without the icon in the status bar. ie. refer to

http://imagebin.org/226494

can the icon circled in red not be displayed? Ultimately I would like to create a service that just sits in the pull down notification.

  • Is there away to make the notification in the pulldown persist ie. if the icon circled in blue (http://imagebin.org/226494) is pressed that the notification still remains?

I am new to android dev and just trying to get to grips with what I can do with notifications.

Thanks

Dino
  • 1,445
  • 4
  • 24
  • 43

3 Answers3

60
  • A1: To remove the status bar icon, use this trick:

    builder.setSmallIcon(android.R.color.transparent); //Tested and worked in API 14
    
  • A2: To make persistant Notification, add this line:

    builder.setOngoing(true)
    
Mixaz
  • 4,068
  • 1
  • 29
  • 55
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • How do you set this on an older API? API8 has no Builder. – Zelter Ady Dec 12 '12 at 14:10
  • 6
    @ZelterAdy You can use [`NotificationCompat.Builder`](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html), a class in the [Support Library](http://developer.android.com/tools/extras/support-library.html) that provides _almost_ the same functionality `Notification.Builder` provides. – iTurki Dec 12 '12 at 16:20
  • `NotificationCompat.Builder` **Tutorial** -> http://stackoverflow.com/questions/13902115/how-to-create-a-notification-with-notificationcompat-builder – Inoy Oct 16 '14 at 11:41
  • 2
    The transparent icon, predictably, just makes the icon transparent. I'm not sure this is what the OP had in mind and it definitely isn't what the question is asking / answer claims. – Prime624 Jan 22 '19 at 05:32
  • does this notification persist even if the app is closed ? – jashgopani Jun 01 '20 at 07:08
3

Take a look at the ongoing event flag. I believe this can create ongoing notifications similar to the system's wifi and usb connection notifications.

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
0
  public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Hello  Toast", Toast.LENGTH_SHORT).show();
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
       /* Intent launchIntent =context.getPackageManager().getLaunchIntentForPackage("com.sonisony.clarityitapp");
            context.startActivity(launchIntent);*/
        Intent i = new Intent(context, CheckAppActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,i,0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"TestAndro")
                .setContentTitle("Andro Open Test Alert")
                .setContentText("Hello Hello ...........")
                .setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
//                for Persistent
                .setOngoing(true);



        NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context);
        notificationCompat.notify(123,builder.build());
    }
}
SHUBHAM SONI
  • 71
  • 2
  • 9