BroadcastReceiver not firing on notification action click

被刻印的时光 ゝ 提交于 2020-06-16 05:06:48

问题


I am trying to create a simple notification with a button (action) defined to it. I have managed to display it properly and create a PendingIntent for my action. I have also created a BroadcastReceiver which is supposed to be called when my action is clicked. But it's onReceive() method does not get called. I have no idea why. I also registered BroadcastReceiver in AndroidManifest.xml

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    Button notify = (Button) findViewById(R.id.notify);

    notify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent dismissIntent = new Intent("action1");
            dismissIntent.setAction("action1");
            PendingIntent action1intent = PendingIntent.getBroadcast(MainActivity.this, (int) System.currentTimeMillis(), dismissIntent, PendingIntent.FLAG_CANCEL_CURRENT);

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_menu)
                            .setContentTitle("My notification")
                            .setContentText("Hello World!")
                            .addAction(R.drawable.ic_menu, "action 1", action1intent);

            Intent resultIntent = new Intent(MainActivity.this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            mNotificationManager.notify(0, mBuilder.build());
        }
    });

}

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("log", "Receiving somthn");

        String whichAction = intent.getAction();

        if (whichAction.equals("action1")) {
            MainActivity.makeToast("This is action 1", context);
        }
    }
}


public static void makeToast(String string, Context context) {
    Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.orglce.notification">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name=".MainActivity$Receiver">
        <intent-filter>
            <action android:name="com.example.orglce.notification.BROADCAST" />
        </intent-filter>
    </receiver>

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


回答1:


First, if you are going to use .MainActivity$Receiver, then Receiver needs to be a static class.

Second, dismissIntent is (redundantly) using action1 as the action string, but the <intent-filter> uses com.example.orglce.notification.BROADCAST. These do not match. I recommend getting rid of the <intent-filter>, getting rid of the action1, and using new Intent(this, Receiver.class) to create an explicit Intent to identify your BroadcastReceiver.



来源:https://stackoverflow.com/questions/38919974/broadcastreceiver-not-firing-on-notification-action-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!