3

我正在为用户收到好友请求时构建通知。通知有一个接受和拒绝按钮,根据用户单击的内容,启动的活动将处理请求。无论如何,当用户单击接受时,启动的活动应该收到标题为接受的请求,反之亦然。但是出于某种奇怪的原因,无论我单击接受还是拒绝,请求字符串都会返回拒绝。我什至将拒绝请求字符串注释掉以发送接受或为空,但它仍然返回拒绝。这太荒谬了。这就是我所做的。

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);

    }

这是接收意图的活动

  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();
        }

但是每次我单击字符串的任何按钮都会返回下降。这真的很奇怪。

4

1 回答 1

6

弄清楚了。问题就在这里

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

两者请求代码相同。它需要有所不同。这有效:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    PendingIntent pIn = PendingIntent.getActivity(this, 1, in, 0);
于 2013-08-11T14:19:39.003 回答