Уведомление BroadcastReceiver не работает в действии - PullRequest
0 голосов
/ 10 апреля 2020

У меня есть уведомление, и я пытаюсь запустить метод в действии при нажатии определенной кнопки c в уведомлении. Я создаю broadcastReceiver в onCreate:

    //broadcast receiver stuff
    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Code to run
            if (intent.getAction().equals("stop")){
                StartButtonClick();
            }
        }
    };
    registerReceiver(broadcastReceiver, new IntentFilter("stop"));

Затем я создаю уведомление в том же упражнении (но не в onCreate):

private void ShowNotification(){
    //add intent for app to resume when notification is clicked
    PendingIntent reopenAppIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

    //add intent for app to stop service when stop button in notification is clicked
    //this is the intent I am creating for the notification button
    Intent stopIntent = new Intent("stop");
    PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    //create notification
    NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setContentTitle("Messaging in progress")
            .setContentText("User is late")
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setSmallIcon(R.drawable.ic_message)
            .setContentIntent(reopenAppIntent)
            .addAction(R.drawable.ic_message, "Stop", stopPendingIntent); //here I am adding the intent for the butt that I want to activate the broadcastReceiver
    notificationManager.notify(1, notification.build());
}

Когда я нажимаю кнопку в уведомлении , Ничего не произошло. Я даже поставил точку останова в if-заявке broadcastReceiver, и при нажатии кнопки ничего не происходило.

1 Ответ

1 голос
/ 10 апреля 2020

Попробуйте это

PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);

вместо

PendingIntent stopPendingIntent = PendingIntent.getService(this, 1, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...