dwylem
dwylem

Reputation: 53

Android notification action buttons are not working when app is killed

I am creating my first android app, it is a vocabulary quiz. I am trying to send notification every hour (using PeriodicWorkRequest - WorkManager). I noticed that when the app gets killed, the notification keeps fire up, but the actions buttons are not working.

This is my send notification method inside my Worker class:

public void sendNot(Word word, int wordNum) {
            Intent activityIntent = new Intent(getApplicationContext(), WordStatsActivity.class);
            activityIntent.putExtra("word_exists", word);
            PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext()
                    , 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Intent knewBroad = new Intent(getApplicationContext(), MainActivity.NotificationBroadcast.class);
            knewBroad.setAction(KNEW_WORD);
            knewBroad.putExtra("word", word);
            PendingIntent knewIntent = PendingIntent.getBroadcast(getApplicationContext(),
                    0, knewBroad, PendingIntent.FLAG_UPDATE_CURRENT);

            Intent didntKnowBroad = new Intent(getApplicationContext(), MainActivity.NotificationBroadcast.class);
            didntKnowBroad.setAction(DIDNT_KNOW_WORD);
            didntKnowBroad.putExtra("word", word);
            PendingIntent didntKnow = PendingIntent.getBroadcast(getApplicationContext(),
                    0, didntKnowBroad, PendingIntent.FLAG_UPDATE_CURRENT);

            Intent stopShowBroad = new Intent(getApplicationContext(), MainActivity.NotificationBroadcast.class);
            stopShowBroad.setAction(NOTIFCATION_OFF);
            stopShowBroad.putExtra("word", word);
            PendingIntent stopShowIntent = PendingIntent.getBroadcast(getApplicationContext(),
                    0, stopShowBroad, PendingIntent.FLAG_UPDATE_CURRENT);


            Notification notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_1_ID)
                    ...
                    .addAction(R.mipmap.ic_launcher, "know", knewIntent)
                    .addAction(R.mipmap.ic_launcher, "don't know", didntKnow)
                    .addAction(R.mipmap.ic_launcher, "don't show again", stopShowIntent)
                    ...
                    .build();
            notificationManager.notify(word.getId(), notification);
        }

Let me know if you need more info

Upvotes: 2

Views: 645

Answers (2)

Thomas
Thomas

Reputation: 471

Try to see if you get errors in your Logcat.

When the app is killed, all your objects are destroyed. When you click on your action, they have to be newly instantiated. You probably have a NullPointerException somewhere where it was obvious for you that the object isn't null when using in the normal lifecycle, but that became null because you don't instantiate it again in your BroadcastReceiver.

Upvotes: 0

Digvijay Wagh
Digvijay Wagh

Reputation: 21

Make sure in your push notification payload, notification key must not be present. Only data key will be present in payload body.

Not use this(Sample)

{
    "to": "a6345d0278adc55d3474f5",
    "data": {
        "message": "Hello World!"
    },
    "notification": {
        "body": "Hello World \u270c",
        "badge": 1,
        "sound": "ping.aiff"
    }
}

instead use this(Sample):

{
    "to": "a6345d0278adc55d3474f5",
    "data": {
        "message": "Hello World!"
    }
}

Upvotes: 1

Related Questions