0

I'm attempting to make a notification that has two buttons which do two different things by running two different activities. The notification and its buttons appear on the notification bar fine, however pressing on either of the buttons fails to launch the designated activity.

Here is the code:

private void addNotification() {
    int requestID = (int) System.currentTimeMillis();

    NotificationCompat.Builder builder = (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ibutton)
            .setContentTitle("Timer running..")
            .setContentText(timerString);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    //Add buttons to notification
    //First make the intent
    Intent pauseIntent = new Intent(this, pauseActivity.class);
    //Wrap it in pending intent to trigger later
    PendingIntent pausePendingIntent = PendingIntent.getActivity(this, requestID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //Then add the action to your notification
    builder.addAction(R.drawable.pause, "Pause", pausePendingIntent);

    //Same again for stop button
    Intent stopIntent = new Intent(this, stopActivity.class);
    PendingIntent stopPendingIntent = PendingIntent.getActivity(this, requestID, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.addAction(R.drawable.stop, "Stop", stopPendingIntent);

    // Add as notification
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mNotificationId = 001;
    notificationManager.notify(mNotificationId, builder.build());


}

EDIT: An example of one of the activities I am trying to launch:

public class stopActivity {
  public stopActivity() {
    MainActivity.stopped = true;
  }
}

If there is any easier way of simply changing the value of a variable in MainActivity from the button in the notification bar then I am also keen to hear that but as far as I can tell the only option is to launch a new activity

James
  • 67
  • 1
  • 10
  • Are you sure you're not looking at an old version of your app? Calling .class on an instance is not supposed to compile. Try changing this to the Class instead of an instance like you did with the MainActivity. – Sander Jan 19 '17 at 16:53
  • I just tested your code and it worked perfectly. pauseActivity and stopActivity are the activity names? – Anindita Pani Jan 19 '17 at 19:42
  • Have you added `pauseActivity` and `stopActivity` to your manifest? – David Wasser Jan 22 '17 at 16:37
  • @Sander `pauseActivity` and `stopActivity` are obviously class names. They don't follow the convention that class names begin with an uppercase letter, but they are class names nonetheless. – David Wasser Jan 22 '17 at 16:39
  • If they are indeed the class names there seems to be nothing wrong with the notification. Have you tried directly starting those activities from your MainActivity? – Sander Jan 23 '17 at 17:07
  • yes sorry they are the class names I am trying to start. Yes calling them from MainActivity with 'new stopActivity();' works fine. – James Jan 24 '17 at 09:49
  • @James This may sound rude but it's meant in a constructive way. It might be better if you read a little more about how Android works, for instance [here](https://developer.android.com/guide/components/fundamentals.html) in the official documents. To help with your question, your StopActivity class is a normal class and not an Activity because it doesn't extend Activity, updating a variable in your MainActivity would be easier if you created a Broadcastreceiver and whatever you're doing in your MainActivity would most likely belong in a Service if it's updated from a Notification. – Sander Jan 24 '17 at 11:24

1 Answers1

0

EDIT: An example of one of the activities I am trying to launch:

public class stopActivity { public stopActivity() { MainActivity.stopped = true; } }

This is not an Activity. An Activity extends Activity!

This is just a POJO class and you cannot start it by putting it in a PendingIntent the way you are doing.


You would be better off by just using an Intent for MainActivity with an "extra" to change a value in it. Here's an example:

Intent pauseIntent = new Intent(this, MainActivity.class);
pauseIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pauseIntent.putExtra("pause", true);
PendingIntent pausePendingIntent = PendingIntent.getActivity(this, requestID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Then add the action to your notification
builder.addAction(R.drawable.pause, "Pause", pausePendingIntent);

Then, in MainActivity.onNewIntent():

if (intent.hasExtra("pause")) {
    this.paused = true; // whatever you want to do when "pause" is pressed
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274