Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle extra sends wrong string

I'm building a notification for when a user receives a friend request. The notification has an accept and decline button and depending on what the user clicks the activity started will handle the request. Anyway when a user clicks on accept the activity started should receive a request titled accept and vice versa. However for some freak reason, whether I click accept or decline, the request string returns decline. I even comment out the decline request string to on send accept or null, but it still returns decline. It's just ridiculous. Here is what I did.

public void newRequest(String name,String id, String relation){

        Intent intent = new Intent(this, Contacts.class);
        Bundle extras= new Bundle();
        extras.putString("request", "accept");
        extras.putString("relation", relation);
        extras.putString("name", name);
        extras.putString("id", id);


        Intent in = new Intent(this, Contacts.class); 
        Bundle extra= new Bundle();
        extra.putString("request", "decline");
        extra.putString("relation", relation);
        extra.putString("name", name);
        extra.putString("id", id);

        intent.putExtras(extras);
        in.putExtras(extra);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        PendingIntent pIn = PendingIntent.getActivity(this, 0, in, 0);

        Notification noti = new NotificationCompat.Builder(this)
            .setContentTitle(name+" sent you have received a friend request")
            .setContentText("Reflap").setSmallIcon(R.drawable.fav)
            .setContentIntent(pIntent)
            .addAction(0, "Accept", pIntent)
            .addAction(0, "Decline", pIn)
            .build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(5, noti);

    }

This is the activity that receives the intent

  Intent intent=getIntent();
        //userName=intent.getStringExtra("username");
        Bundle extras=intent.getExtras();
        ///*if(extras.getString("request")!=null){
        try{
        requester=extras.getString("request");
        senderId=extras.getString("id");
        namer=extras.getString("name");
        relation=extras.getString("relation");
        System.out.println("Starting to perform an accept with "+namer+" and user id "+senderId+" and request is "+requester);
        }catch(Exception e){
            e.printStackTrace();
        }

But every time whatever button I click the string returns decline. It's really weird.

like image 992
Michael Nana Avatar asked Mar 13 '26 16:03

Michael Nana


1 Answers

Figured it out. The problem was here

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    PendingIntent pIn = PendingIntent.getActivity(this, 0, in, 0);

Both request code where the same. It needed to be different. This works:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    PendingIntent pIn = PendingIntent.getActivity(this, 1, in, 0);
like image 129
Michael Nana Avatar answered Mar 16 '26 06:03

Michael Nana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!