0

I have an Intent that I initiate like this:

notificationIntent = new Intent(context, HomeActivity.class);

This intent is attached to an ongoing notification.

Now, in addition to the class opened when clicking the intent, I want to add an action string to the intent, so that when the notification clicked my custom BroadcastReceiver that listens to the same action string will trigger.

notificationIntent.setAction(context.getString(R.string.notification_clicked_action_string));

Problem is, for some reason, the BroadcastReceiver is not called, and I have other BroadcastReceiver that I registered problematically like this and they work fine.

So, it is a problem to have both a class and an action in an intent?

Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101

1 Answers1

1

So, it is a problem to have both a class and an action in an intent?

No, that is perfectly fine.

However, unless you have a very strange naming system, HomeActivity is an activity. That means that new Intent(context, HomeActivity.class) identifies that activity, and you are hopefully using that with PendingIntent.getActivity(). You cannot have a BroadcastReceiver respond to startActivity(), which is what will be called when the PendingIntent is invoked.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ohh I see, so is there any way to send a broadcast when clicking the notification? – Ofek Agmon Jun 14 '16 at 19:22
  • 1
    @OfekAgmon: Sure. Use an `Intent` that matches the `BroadcastReceiver`, then use `PendingIntent.getBroadcast()`. What you *can't* do is have a *single* `PendingIntent` that invokes both an activity and a receiver. You could have your activity send the broadcast in `onCreate()`, though. – CommonsWare Jun 14 '16 at 19:27