androidandroid-intentwear-osandroid-wear-notification

Android Wear Intent from notification not firing


I am writing a small Android Wear App, and part of what it does is creating an ongoing notification with two actions: 1. Open the app again. 2. Close (clear the state) of the app.

The first action (open) works perfectly (ie.: I press the button, the action is executed). But the second action does not fire anything.

Here is the code of the notification itself:

    // First action: open app's main activity
    Intent actionIntent = new Intent(this, MainActivity.class);
    PendingIntent actionPendingIntent =
            PendingIntent.getActivity(this, 0, actionIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Action action =
            new NotificationCompat.Action.Builder(R.drawable.close_button,
                    getString(R.string.open_app), actionPendingIntent)
                    .build();

    // Second action: clear the state of the app, by firing a service which will do so
    Intent tnsIntent = new Intent(this, TimerNotificationService.class);
    PendingIntent tnsPendingIntent =
            PendingIntent.getActivity(this, 0, tnsIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Action closeAppAction =
            new NotificationCompat.Action.Builder(R.drawable.close_button,
                    getString(R.string.close_app), tnsPendingIntent)
                    .build();

    return new  NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.common_signin_btn_icon_dark)
            .setContentTitle(getString(R.string.time_tracking_on))
            .setContentText("Tap to open app")
            .setWhen(System.currentTimeMillis())
            .setOngoing(true)
            .extend(new NotificationCompat.WearableExtender().addAction(action).addAction(closeAppAction))
            .setLocalOnly(true)
            .build();

Note: I tried instantiating the tnsIntent in another way Intent tnsIntent = new Intent(ACTION_TERMINATE_TRACKING, null, this, TimerNotificationService.class); but it did not change anything.

The service looks like that:

public class TimerNotificationService extends IntentService {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        clearSomeState();
    }
}

I ran the app in debugging mode and put a breakpoint in the onHandleIntent() of the service and I did not hit the breakpoint, so the service does not even receive the intent. Did I miss some intent-registration call that I should be doing somewhere for the service maybe? (in the manifest?)


Solution

  • If tnsPendingIntent is launching a Service, it should be set from PendingIntent.getService, not PendingIntent.getActivity as your code snippet shows. (reference)