Christopher Treanor
Christopher Treanor

Reputation: 515

Notification Builder can't accept "this" as replacement for "context", why?

How do I resolve this problem? If I replace it with this. It created more errors on the builder. How do I solve it?

All codes in the java class. In this case, the functionality revolves around the onStartCommand.

 public class MyNotificationService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "OnStartCommand()", Toast.LENGTH_SHORT).show();
        NotificationManager notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationintent = new Intent(this, Reminder_2.class);
        PendingIntent pendingintent = PendingIntent.getActivity(this, 0, notificationintent, 0);
        int icon=R.drawable.ic_launcher;
        long when=System.currentTimeMillis();
        Notification.Builder builder = new Builder(this);
        builder.setContentIntent(pendingintent);
        builder.setAutoCancel(true);
        builder.setSmallIcon(icon);
        builder.setWhen(when);
        builder.setTicker("Notification");
        builder.setContentTitle("Title");
        builder.setContentText("Content");
        Notification notification = builder.build();
        notificationmanager.notify(033, notification);
        return super.onStartCommand(intent, flags, startId);
    }


}

Upvotes: 1

Views: 5064

Answers (3)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this
Notification.Builder builder = new Notification.Builder(this);

Upvotes: 2

Solomon
Solomon

Reputation: 490

I think you've used the wrong Builder class, please try below,

Notification noti = new Notification.Builder(this)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

Reference: android developer site, Notification.Builder

Hope can help you.

Upvotes: 0

nhgrif
nhgrif

Reputation: 62062

Use getApplicationContext() instead of this

Upvotes: 1

Related Questions