4

I'm trying to set a notification in my android app that will simply say "It worked", but I need my app to have compatibility all the way down to API 1. I'm really confused on how to do this though. There are old tutorials that are deprecated, and there are new tutorials that don't support older API levels. According to this SO question, I should use NotificationCompat.Builder. There is an example that I'm using, but I don't fully understand the code.

Out of this code:

Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
        YOUR_PI_REQ_CODE, notificationIntent,
        PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) ctx
        .getSystemService(Context.NOTIFICATION_SERVICE);

Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);

builder.setContentIntent(contentIntent)
            .setSmallIcon(R.drawable.some_img)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
            .setTicker(res.getString(R.string.your_ticker))
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle(res.getString(R.string.your_notif_title))
            .setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();

nm.notify(YOUR_NOTIF_ID, n);

I get red lines under: ctx, YOUR_PI_REQ_CODE, and YOUR_NOTIF_ID

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204

2 Answers2

2

ctx is Context. It can pass instead your Activity.

YOUR_PI_REQ_CODE is PendintIntent Request Code. It can be any int constant.

YOUR_NOTIF_ID is Notification id. It can be any int constant too.

vasart
  • 6,692
  • 38
  • 39
2

The ctx variable is intended to be an Android context -- often an Activity (or actually a class that extends Activity).

You should do a little research on the PendingIntent class to understand YOUR_PI_REQ_CODE but you need to determine what to put here; it's your pending intent request code.

You should also research the NotificationManager notify() method to determine what you want to use as your notification ID.

mah
  • 39,056
  • 9
  • 76
  • 93
  • According to the SO answer you linked, the code you're using is only supported in API v4 and newer. That being said... why do you feel you need to support older than that? Anyone with a phone that old is pretty unlikely to be installing new applications. – mah Oct 12 '12 at 00:03
  • Bah. I guess you're right hahaha. Thanks. I appreciate the effort. Quick question. Do you know what method makes a notification "not cancellable"? Kind of how the voicemail notification works. It won't go away until you check all of your voicemail. Thanks – EGHDK Oct 12 '12 at 00:07
  • After you build the `Notification` you can modify its public `flags` member (no method, just a public int you change) to set various flags. FLAG_ONGOING_EVENT (see http://developer.android.com/reference/android/app/Notification.html#FLAG_ONGOING_EVENT ) should be set for the use case you describe. – mah Oct 12 '12 at 00:10
  • Thanks. You know your stuff when it comes to notifications. Appreciate the help once more. – EGHDK Oct 12 '12 at 00:11