Pergunta

I have an Android Service that creates a notification and corresponding status icon . When the user reacts to the notification, I'd like the intent to be delivered to my Service. That all seems pretty straightforward.

I can create the notification and the icon appears in the status bar, and when I pull down the windowshade I see my notification. When I tap on the notification, nothing happens. When I say nothing, what I mean is that the intent isn't delivered to my Service.

What I'm sensing is that I've incorrectly set something either in the Notification, the Intent, or the PendingIntent but I'm struggling to figure out which one(s) is/are incorrect.

Here's the code (all in the Service class):

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand(): intent=" + intent + ", startId=" + startId);
    Log.d(TAG, LKServiceMessages.dumpExtras(intent));

    String command = LKServiceMessages.getMessageKind(intent);


    if (command.equals(K_STATUS_NOTIFICATION)) {
        cancelNotification(0);
        command = null;
    } else {
        displayStatusNotification();
    }

    if (command == null) {
        Log.d(TAG, "Non-library command received - ignoring...");
    } else {
        Log.d(TAG, "Handling action - partially processing this action");
    }

    return START_REDELIVER_INTENT;
}

private void displayStatusNotification() {
    Intent intent = new Intent(K_STATUS_NOTIFICATION,null,this,LKService.class);

    PendingIntent pIntent = PendingIntent.getService(LKService.this, 0, intent, 0);

    mNotification = new NotificationCompat.Builder(this)
        .setContentTitle("Bacon")
        .setContentText("Bacon is good for you. Tap to dismiss.")
        .setSmallIcon(R.drawable.ic_status)

        .addAction(R.drawable.ic_status, "Notification getAction()", pIntent)

        .setAutoCancel(true)
        .setDeleteIntent(pIntent)

        .build();

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, mLeafNotification);
}

private void cancelNotification(int notificationId) {
    if (Context.NOTIFICATION_SERVICE != null) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
        nMgr.cancel(notificationId);
    }
}

I've done something wrong, but I can't figure out what. My initial thought is that the PendingIntent is wrong, but there's a lot of moving parts here that I don't fully understand.

Any thoughts?

Foi útil?

Solução

When building the notification, you are calling

.setDeleteIntent(pIntent)

This sets the Intent that will be used when the user deletes the notification. It sounds like you want to call

.setContentIntent(pIntent)

instead. This sets the Intent that will be used when the user clicks on the notification.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top