Search code examples
androidandroid-intentbundleextrasandroid-remoteview

Dyanamically adding RemoteViews in custom notification in for loop, issue in setting pendingintent


I am creating a custom notification and adding buttons dynamically using this code:

RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.custom_notification);

    for (int i = 0; i < 100; i++) {

        Intent mybuttonsIntent = new Intent(this, NotifyActivityHandler.class);
        RemoteViews rv = new RemoteViews(this.getPackageName(), R.layout.button_layout);
        Bundle b = new Bundle();
        b.putInt("abcd", i);
        mybuttonsIntent.putExtras(b);
        rv.setOnClickPendingIntent(R.id.button, PendingIntent.getActivity(this, 0, mybuttonsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        remoteViews.addView(R.id.rlMainNotification, rv);
    }
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pIntent)
            .setContent(remoteViews);
    NotificationManager notificationmanager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationmanager.notify(0, builder.build());

Here's button_layout.xml:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_weight="1" />

In NotifyActivityHandler, I am using this code to get Int:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int level  = extras.getInt("abcd");
            Toast.makeText(this, "testing" + level, Toast.LENGTH_LONG).show();

But the problem is that every button passes the same Int i.e 99 If instead of using R.id.button I use rv.getLayoutId() then the buttons do not respond. I want that every button should pass the value assigned to it respectively in the for loop.

Edit: on request of a commenter, here's the xml of custom_notification

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">

<LinearLayout
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/autoIcon"
        android:tint="@color/themeColor"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
android:src="@drawable/ic_brightness_medium_white_48dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/rlMainNotification"
    android:layout_width="0dp"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:orientation="horizontal"
    android:layout_height="match_parent"
    android:paddingRight="4dp"
    android:paddingLeft="2dp"
    android:layout_weight="9"
    >

</LinearLayout>

</LinearLayout>

Solution

  • change this line to:

      rv.setOnClickPendingIntent(R.id.button, PendingIntent.getActivity(this, 0, mybuttonsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    

    to

      rv.setOnClickPendingIntent(R.id.button, PendingIntent.getActivity(this, i, mybuttonsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    

    basically change 0 to i.