【问题标题】:My notification is not retrieving the extras in pending intent我的通知未检索待处理意图中的附加内容
【发布时间】:2020-04-21 20:29:13
【问题描述】:

我已经尝试了大多数解决方案,但点击通知时我仍然没有得到额外的解决方案 我试过 1Why the PendingIntent doesn't send back my custom Extras setup for the Intent? 2 Intent.getExtras() always returns null

                String itemID = dataSnapshot.child("itemID").getValue(String.class);
                Intent intent = new Intent(UserHomeActivity.this, MapsActivity.class);
                intent.putExtra("id", itemID);
                intent.setAction(itemID);
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

                int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
                PendingIntent pendingIntent = PendingIntent.getActivity(UserHomeActivity.this, 0,
                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ){
                    CharSequence name ="001";
                    NotificationChannel notificationChannel = new NotificationChannel(Channel_ID, name,NotificationManager.IMPORTANCE_HIGH );
                    notificationChannel.setDescription("This is description");

                    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                    notificationManager.createNotificationChannel(notificationChannel);

                    Notification.Builder builder = new Notification.Builder(getApplicationContext(),Channel_ID);
                    builder.setSmallIcon(R.mipmap.ic_launcher)
                            .setContentText(text)
                            .setContentTitle("New Price Added")
                            .setContentIntent(pendingIntent)
                            .setPriority(Notification.PRIORITY_DEFAULT);
                    NotificationManagerCompat  notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
                    notificationManagerCompat.notify(001,builder.build());

                }else {
                    Notification.Builder builder = new Notification.Builder(getApplicationContext());
                    builder.setSmallIcon(R.mipmap.ic_launcher)
                            .setContentText(text)
                            .setContentTitle("New Price Added")
                            .setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setPriority(Notification.PRIORITY_MAX);
                    NotificationManagerCompat  notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
                    notificationManagerCompat.notify(uniqueInt,builder.build());

这是我的代码获取额外内容

        if(extras != null){
                // extract the extra-data in the Notification
                String msg = extras.getString("id");
                String sss = getIntent().getParcelableExtra("id");
            Toast.makeText(this, sss+" ids  "+msg+ "     "+extras.toString(), Toast.LENGTH_SHORT).show();

        }
        imageButton = findViewById(R.id.call);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
                phoneIntent.setData(Uri.parse("tel:0818283848"));
                startActivity(phoneIntent);
            }
        });     ```


【问题讨论】:

  • 请问我做错了什么
  • 你应该使用 getIntent().getStringExtra("id"); , getIntent().getExtras() 为空

标签: java android


【解决方案1】:

您是否尝试过调试附加功能?尝试设置断点并查看您通过意图收到的内容。检查额外的是否真的通过了。

如果你没有收到额外的,那么你的意图是错误的。 如果您收到的是额外的,那么您如何访问额外的逻辑是错误的。

【讨论】:

  • 是的,所有的数据都通过了,我已经尝试了所有我知道的可能的方法来获得额外的东西,它总是返回 null
  • 您是否尝试通过调试器运行并查看您的待处理 Intent 有什么?要解决问题,您需要找到错误所在的一侧。在触发意图之前或接收之后。
猜你喜欢
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
相关资源
最近更新 更多