-1

I have added a some itent like AACTION_POWER_CONNECTED in my manifest file, I want to get Notification when charger is connected. This my code.

    public class AwesomeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Inside onReceive", Toast.LENGTH_LONG).show();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.ic_launcher);
        mBuilder.setContentTitle("This is A Notification");
        mBuilder.build();

    }
}

I am getting the Toast each time when I connect my charger but not getting the notification. As I am completely new in Android, I don't know how to achieve this.

Kalpesh Patel
  • 1,638
  • 1
  • 20
  • 35
Firdoesh khan
  • 37
  • 1
  • 1
  • 9

1 Answers1

0

Try this code:

public class AwesomeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Inside onReceive", Toast.LENGTH_LONG).show();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setSmallIcon(R.drawable.ic_launcher);
        mBuilder.setContentTitle("This is A Notification");

        NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(mId, mBuilder.build());
    }
}

Actually you created Notification object but never fired it :) So, you were simply remaining with the notify() to call.

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174