Teh john
Teh john

Reputation: 41

Notification at end of CountDownTimer

I am trying to make notification to tell me that my CountDownTimer has ended, But im having some trouble. At the moment i have established just a test notification (i think) But i don't know how to call for the notification to work. Here is what i have at the moment:

public void onFinish() {
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(TestTimer.this);
      mBuilder.setContentTitle("Notification Alert, Click Me!");
      mBuilder.setContentText("Hi, This is Android Notification Detail!");
     TextView timer=(TextView)findViewById(R.id.mTextField);
    timer.setText("Seconds remaining: 0 ")

This is called once the timer = 0, and i want to know if its possible to call for a notification when it does.

Thanks in advance for any help, If you need any more code feel free to ask.

Upvotes: 0

Views: 1495

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

To enable notification you need to call PendingIntent so notification will work.

try this in your onFinish

 public void onFinish() {
                 NotificationManager  notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                 Notification  myNotification = new Notification(R.drawable.ic_launcher, "Notification!", System.currentTimeMillis());
                    Context context = getApplicationContext();
                    String notificationTitle = "Exercise of Notification!";
                    String notificationText = "http://android-er.blogspot.com/";
                    Intent myIntent = new Intent(MainActivity.this, MainActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,   myIntent, Intent.FILL_IN_ACTION);
                    myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
                    myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
                    notificationManager.notify(1, myNotification);
             }

Upvotes: 5

Related Questions