5

This is my code:


NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);

    //Instantiate the notification

    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification.Builder builder = new Notification.Builder(c)
                                .setTicker(tickerText)
                                .setWhen(when)
                                .setContentTitle("Test Notification")
                                .setContentText(arg1.getStringExtra("info"))
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setAutoCancel(true);
    Notification notification = builder.getNotification();
    mNotificationManager.notify(88, notification);

It works find, but using Notification notification = builder.getNotification(); is deprecated. as I should be doing Notification notification = builder.build();. Problem is Eclipse isn't recognizing it as such, meaning it won't let me compile. The doc is clear that build() exists and its the preferred method, but its not working on my end. I would like to use non-deprecated code, so any help will be much appreciated.

imports


import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

Be aware that import android.app.Notification.Builder; is saying its not being used.

Andy
  • 10,553
  • 21
  • 75
  • 125
  • 1
    Are you targeting API 16? `build()` is new in Jelly Bean. – Cat Jul 07 '12 at 03:51
  • Haha, not JellyBean. ICS. Though on a side note if you wouldn't mind answering a quick question unrelated to this one. Should most of the API's from ICS work for JellyBean. I ask because I would like to upgrade to the new OS and try it, but not if my dev phone will not support what I am building my app on. – Andy Jul 07 '12 at 03:53
  • Any APIs that are in ICS that aren't in Jelly Bean will only be deprecated, so they will still be safe to use in API 16. But I would run your app on the 4.1 emulator before upgrading your device... just in case. – Cat Jul 07 '12 at 03:55

2 Answers2

2

If you want develop for version SDK lower than 11, you can use this code instead android.app.Notification.Builder class for creation notification:

private void createNotification(){
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "Hello", System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, YourActivity.class);

    Random r = new Random();
    int notificationId = r.nextInt();
    notificationIntent.putExtra("n_id", notificationId);
    PendingIntent contentIntent = PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.setLatestEventInfo(this, "Party", "Welcome!", contentIntent);
    mNotificationManager.notify(notificationId, notification);      
}

You can cancel this notification in YourActivity like this:

public class YourActivity extends Activity{ 

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity);

    int notificationId = getIntent().getIntExtra("n_id", -1);

    if (notificationId!=-1) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notificationId);
    }
  }
}
valerybodak
  • 4,195
  • 2
  • 42
  • 53
  • What does the pipe and equals sign mean `|=`. Is it java specific. But thanks. – Andy Nov 20 '12 at 18:30
  • Yes. It's one of the logical operators in Java. The **Bitwise inclusive OR** ( | ) operator performs the bitwise inclusive OR operation on each parallel pair of bits of two operands. In each pair, the result is 1, if either first or second bit is 1 (or both are 1). Otherwise the result is 0. When used with compound statement like **x |= y** it is to be read as **x = x | y** – valerybodak Nov 21 '12 at 08:10
1

This only happens when, You are compiling your app code against the version which does not hold required method/api (in your case it is build()).

If build is not available in the version you are compiling with, I will suggest you to compile with higher version of Android, You always have minimum sdk version in manifest for backward compatibility.

Thanks

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • But my sdk version is ICS, which should have this. Though I see what you are saying. – Andy Jul 07 '12 at 20:17