简体   繁体   中英

Button click from Notification is not Firing Broadcast Receiver

I have created an android application where I want to show a toast on click of a button on Notification but somehow its not working for me, could anybody help please. Below are the three files, which I used in my application. Thanks in Advance

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>

I have another custom_notificatoin.xml for notification design, which I have not added here.

Now is not possible to static register the receiver on AndroidManifest, you must register on code through the method context.registerReceiver on your code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM