9

In the below posted code i am create a notification with a customized layout. the layout of the notification contains three action buttons.

the problem i have now is, i can not reference any of the buttons in the code so that I can navigate to another activity based on the action button clicked.what i am trying to do is when Action button 1 is clicked then Activity 1 shows up, when Action button 2 is clicked then Activity 2 shows up and so on.

Please let me know how to reference the views in customized layout of the notification?

code:

public class MainActivity extends AppCompatActivity {

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

        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
        String notification_title = "Notification_Title";
        String notification_text = "Notification_Text";

        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationReply.class);
        // Send data to NotificationView Class
        intent.putExtra("title", notification_title);
        intent.putExtra("text", notification_text);
        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // Set Icon
                .setSmallIcon(R.mipmap.ic_launcher)
                // Set Ticker Message
                .setTicker("Ticker")
                // Dismiss Notification
                .setAutoCancel(true)
                // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                // Set RemoteViews into Notification
                .setContent(remoteViews);

        Intent intentAction1 = new Intent(this, ActAction1.class);
        PendingIntent pendingIntentActAction1 = PendingIntent.getBroadcast(this, 1,intentAction1, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btn_action1, pendingIntentActAction1);

        Intent intentAction2 = new Intent(this, ActAction2.class);
        PendingIntent pendingIntentActAction2 = PendingIntent.getBroadcast(this, 2,intentAction2, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btn_action2, pendingIntentActAction2);

        Intent intentAction3 = new Intent(this, ActAction3.class);
        PendingIntent pendingIntentActAction3 = PendingIntent.getBroadcast(this, 3,intentAction3, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setPendingIntentTemplate(R.id.btn_action3, pendingIntentActAction3);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());
    }
}
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Try getting reference of `btn_action1` as: `PendingIntent pendingIntentActAction1; / *leave it null, just to check if this line is the cause of error */ remoteViews.setOnClickPendingIntent(R.id.btn_action1, pendingIntentActAction1);` – Uddhav P. Gautam Aug 01 '17 at 05:45
  • If this is the case then we can research on provide value of `pendingIntentActAction1` – Uddhav P. Gautam Aug 01 '17 at 05:47
  • The problem with your code is PendingIntent is not properly used for launching the activity. You are sending broadcast instead of getActivity Intent,please find the code below and please give me bounty please refer to my submission if you find it relevant `Intent firstIntent = new Intent(context, FirstActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(appContext, 101, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setPendingIntentTemplate(R.id.btn_action3, pendingIntent);` – Anukalp Katyal Aug 01 '17 at 14:23

3 Answers3

3

The key success for navigation is to use proper pending intent and use different values for your requestCode.

You could receive the event in broadcastReceiver building the pending intent (e,g, with params) that way:

private PendingIntent buildPendingIntent(String someurl) {
        Intent intent= new Intent(mContext, SomeReceiver.class);
        final Uri uri = Uri.parse(someurl);
        intent.setData(uri);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra(KEY, VALUE)

        return PendingIntent.getBroadcast(mContext.getApplicationContext(),
                CONSTANT_FOR_THIS_INTENT_TYPE, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
    }

Or you could navigate to activity just by pending intent:

private PendingIntent buildPendingIntent2() {
        Intent intent = new Intent(mContext, SomeActivity.class);
        intent.putExtra(KEY, VALUE);
        settingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        return PendingIntent.getActivity(mContext.getApplicationContext(),
                                         OTHER_CONSTANT_FOR_OTHER_ACTION,
                                         intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

Please notice that CONSTANT_FOR_THIS_INTENT_TYPE and OTHER_CONSTANT_FOR_OTHER_ACTION must have different values

And after you have this pending intents you should attach them to your views:

view.setOnClickPendingIntent(com.app.btn1, buildPendingIntent1());
view.setOnClickPendingIntent(com.app.btn2, buildPendingIntent2());
dilix
  • 3,761
  • 3
  • 31
  • 55
  • please have a look at the code, i updated it...i just added different values for requestCode and still the problem persists – Amrmsmb Jul 25 '17 at 10:24
  • What happen if you click on your button? Nothing happens or you just open one of the activities? Could you show custom layout pleasE? – dilix Jul 25 '17 at 13:21
  • Try to use https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT instead of FLAG_UPDATE_CURRENT? – dilix Jul 25 '17 at 13:25
2

You have to use RemoteViews#setOnClickPendingIntent() API:

Equivalent to calling setOnClickListener(android.view.View.OnClickListener) to launch the provided PendingIntent.


    Intent firstIntent = new Intent(context, FirstActivity.class);
    PendingIntent firstPendingIntent = PendingIntent.getBroadcast(this, 0, firstIntent, 0);

    RemoteViews notificationView = ...
    notificationView.setOnClickPendingIntent(R.id.first_button, firstPendingIntent);

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • First, instead of `remoteViews.setPendingIntentTemplate(R.id.btn_action3, ...` you should be doing `remoteViews.setOnClickPendingIntent(R.id.btn_action3, ...)`. Secondly, does `ActAction1` extend `Activity`? – azizbekian Jul 25 '17 at 10:05
  • please have a look at the updated code...i used the same code you posted, and yes, all classes extends AppCompatActivity – Amrmsmb Jul 25 '17 at 10:18
1

You have to use RemoteViews#setOnClickPendingIntent() API:

I get your mistake you are trying to launch an activity with PendingIntent.getBroadcast() which only send a broadcast message, so ideally a broadcast receiver should be registered with the same intent filter action. In your case it should ideally be PendingIntent.getActivity

Now you can launch with the following pending intent the flag value was incorrect I can assume for everyone else, hence pending intent wasn't fired.

    Intent firstIntent = new Intent(context, FirstActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(appContext, 101, firstIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT); // For opening activity ideally this should work

If this doesn't work you can try sending a broadcast and listen for it to verify that broadcast is also sent or not:-

PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, firstIntent, PendingIntent.FLAG_ONE_SHOT); // for sending broadcast and listens in broadcast reciever