捆绑额外发送错误的字符串

时间:2013-08-11 12:39:20

标签: android android-intent android-notifications

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

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

但每次点击字符串的任何按钮都会返回拒绝。这真的很奇怪。

1 个答案:

答案 0 :(得分:5)

想出来。问题出在这里

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