简体   繁体   English

BroadcastReceiver 不触发通知操作点击

[英]BroadcastReceiver not firing on notification action click

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.我设法正确显示它并为我的操作创建了一个 PendingIntent。 I have also created a BroadcastReceiver which is supposed to be called when my action is clicked.我还创建了一个 BroadcastReceiver,应该在单击我的操作时调用它。 But it's onReceive() method does not get called.但它的 onReceive() 方法没有被调用。 I have no idea why.我不知道为什么。 I also registered BroadcastReceiver in AndroidManifest.xml我还在 AndroidManifest.xml 中注册了 BroadcastReceiver

MainActivity.java主活动.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 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>

First, if you are going to use .MainActivity$Receiver , then Receiver needs to be a static class. 首先,如果要使用.MainActivity$Receiver ,则Receiver必须是static类。

Second, dismissIntent is (redundantly) using action1 as the action string, but the <intent-filter> uses com.example.orglce.notification.BROADCAST . 其次, dismissIntent是(冗余地)使用action1作为操作字符串,但是<intent-filter>使用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 . 我建议您摆脱<intent-filter> ,摆脱action1 ,并使用new Intent(this, Receiver.class)创建一个明确的Intent来标识BroadcastReceiver

Your manifest has problem.您的清单有问题。 You add action to your notification intent with this name "action 1" you should edit your action name in MainActivity like this:您使用名称“action 1”向通知意图添加操作,您应该在 MainActivity 中编辑您的操作名称,如下所示:

 Intent dismissIntent = new Intent("com.example.orglce.action1");

Edit your receiver like this:像这样编辑您的接收器:

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

Finally edit your if in broadcast receiver like this:最后在广播接收器中编辑您的 if ,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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