Builder notifications in android 2.3

Pretty new in android here :)

I have a notification builder that works without problems if the target version of the application is> 4.0. However, when I switch to 2.3, I get an error message on this line that says: "Notificaiton.Builder cannot be resolved to type".

Notification notification = new Notification.Builder(this)
            .setSmallIcon(drawable_small)
            .setLargeIcon(drawable_big)
            .setWhen(System.currentTimeMillis()).setTicker(content_title)
            .setContentTitle(content_title).setContentInfo(content_info)
            .setContentText(content_text).setContentIntent(pIntent)
            .getNotification();

Now this problem is solved! However, I have one more. It gives me an error for each R (resource), and I have the Import R option. If I import it, it will give me errors on each resource.

setContentView(R.layout.activity_main);
+3
source share
5 answers

Contribute your Notificationlike

     Notification noti = new NotificationCompat.Builder(context)
                    .setSmallIcon(icon_small)
                    .setTicker(message)
                    .setLargeIcon(largeIcon)
                    .setWhen(System.currentTimeMillis())
                    .setContentTitle(title)
                    .setContentText(message)
                    .setContentIntent(contentIntent)
                    //At most three action buttons can be added
                    .setAutoCancel(true).build();

And add the v4 support library to your project as well as import

import android.support.v4.app.NotificationCompat;

NotificationCompat Notification, API level 4 .

: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+9

:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;

// To support 2.3 os, we use "Notification" class and 3.0+ os will use
// "NotificationCompat.Builder" class.
 if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
 notification = new Notification(icon, message, 0);
 notification.setLatestEventInfo(context, appname, message,
 contentIntent);
 notification.flags = Notification.FLAG_AUTO_CANCEL;
 notificationManager.notify(0, notification);

} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();

notificationManager.notify(0 , notification);
}

, .

+4
  • v4 , , " Android" > " "

  • NotificationCompat.Builder

+1
     NotificationManager notificationManager =
            (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
   Notification n = new Notification(R.drawable.ic_launcher,
     "New Message",
     System.currentTimeMillis());
   Context context = getApplicationContext();
   String notificationTitle = "Got new Message";
   String notificationText = "";
   Intent myIntent = new Intent(MainActivity.this,MainActivity.class);
   PendingIntent pendingIntent
     = PendingIntent.getActivity(MainActivity.this, 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
   n.defaults |= Notification.DEFAULT_SOUND;
   n.flags |= Notification.FLAG_AUTO_CANCEL;
   n.setLatestEventInfo(context,
      notificationTitle,
      notificationText,
      pendingIntent);
   notificationManager.notify(1,n);
+1

, :)

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MYDEMOACTIVITY.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

v4 ,

import android.support.v4.app.NotificationCompat;
0

All Articles