Почему уведомление PendingIntent не работает? - PullRequest
0 голосов
/ 07 марта 2020

У меня есть уведомление и кнопка «Стоп». PendingIntent этой кнопки относится к BackgroundTaskService's внутреннему классу NotificationReceiver. Но когда я нажимаю кнопку «Стоп», ничего не происходит. Что не так?

NotificationLab class:

class NotificationsLab{
    /*...Some constructors, variables and other stuff here...*/

    public Notification createNotification(UUID taskId){
        Intent intent= new Intent(mContext, BackgroundTaskService.NotificationsReceiver.class);
        intent.setAction(BackgroundTaskService.CANCEL_TASK_ACTION);
        intent.putExtra(TASK_ID, taskId);
        PendingIntent cancelPendingIntent =
                PendingIntent.getBroadcast(mContext, 0, intent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
                .addAction(R.drawable.ic_file_gray_classic_24dp, "Stop",
                        cancelPendingIntent)
                .setProgress(100, 0, false);
        return builder.build();
    }
}

BackgroundTaskService class:

class BackgroundTaskService extends Service{
    private BroadcastReceiver mReceiver = new NotificationReceiver();
    public IBinder onBind(Intent intent) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(CANCEL_TASK_ACTION);
        registerReceiver(mReceiver, filter);
        return mIBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        filter.addAction(CANCEL_TASK_ACTION);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(CANCEL_TASK_ACTION);
        registerReceiver(mReceiver, filter);
        return super.onStartCommand(intent, flags, startId);
    }
    public static class NotificationsReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("TAG", "You will never see this message!!!");
        }
    }
}

Итак, сообщение что Log.d должен напечатать, никогда не появится. Что я могу с этим сделать?

1 Ответ

0 голосов
/ 07 марта 2020

Проблема заключалась в том, что я забыл добавить свой BroadcastReceiver в манифест.

Просто добавил <receiver android:name=".services.BackgroundTaskService$NotificationsReceiver"/> в манифест

...