Нажатие кнопки из уведомления не является приемником вещания - PullRequest
0 голосов
/ 06 января 2020

Я создал приложение android, в котором я хочу показывать тост по нажатию кнопки «Уведомление», но почему-то у меня это не работает, может кто-нибудь помочь, пожалуйста. Ниже приведены три файла, которые я использовал в своем приложении. Заранее спасибо

MainActivity. Java

public class MainActivity extends AppCompatActivity {
    private NotificationCompat.Builder builder;
    private NotificationManager notificationManager;
    private int notification_id;
    private RemoteViews remoteViews;
    private Context context;

    private final String CHANNEL_ID = "personal_notifications";
    private final int NOTIFICATION_ID = 001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context=this;
        notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        remoteViews= new RemoteViews(getPackageName(),R.layout.custom_notification);

        notification_id = (int)System.currentTimeMillis();
        Intent buttonIntent = new Intent("button_clicked");
        buttonIntent.putExtra("id",notification_id);
        PendingIntent p_button_intent = PendingIntent.getBroadcast(context,123,buttonIntent,0);
        remoteViews.setOnClickPendingIntent(R.id.button, p_button_intent);

        findViewById(R.id.button_show_notif).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createNotificationChannel();
                Intent notification_intent = new Intent(context, MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notification_intent, 0);

                builder = new NotificationCompat.Builder(context,CHANNEL_ID);
                builder.setSmallIcon(R.drawable.ic_sms)
                        .setAutoCancel(true)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                        .setCustomBigContentView(remoteViews)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent);
                notificationManager.notify(notification_id,builder.build());

                }
        });
    }

button_listner

package com.red.testbroadcastreceivers;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class button_listner extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(intent.getExtras().getInt("id"));
        Toast.makeText(context, "Working", Toast.LENGTH_LONG).show();
    }
}

AndroidManifest

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:enabled="true" android:name="button_listner">
            <intent-filter>
                <action android:name="button_clicked"/>
            </intent-filter>
        </receiver>

У меня есть еще один custom_notificatoin. xml для дизайна уведомлений, который я здесь не добавил.

1 Ответ

0 голосов
/ 06 января 2020

Теперь невозможно установить c зарегистрировать приемник в AndroidManifest, необходимо зарегистрироваться по коду с помощью метода context.registerReceiver для вашего кода

https://developer.android.com/reference/android/content/Context.html#registerReceiver (android .content.BroadcastReceiver,% 20 android .content.IntentFilter)

...