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

Mic*_*ana 3 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);

    }
Run Code Online (Sandbox Code Playgroud)

这是接收意图的活动

  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();
        }
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*ana 5

弄清楚了.问题出在这里

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    PendingIntent pIn = PendingIntent.getActivity(this, 0, in, 0);
Run Code Online (Sandbox Code Playgroud)

两者请求代码相同.它需要与众不同.这有效:

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    PendingIntent pIn = PendingIntent.getActivity(this, 1, in, 0);
Run Code Online (Sandbox Code Playgroud)

  • 是的,拥有不同的额外功能并不能使PendingIntents与系统的视角有所不同,因此旧版本会被重用.另一种方法是在数据URI中附加一些唯一的字符串. (4认同)