简体   繁体   中英

My notification is not retrieving the extras in pending intent

i have tried most solution but still i don't get the extras when notification is clicked i tried 1 Why 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());

Here is my Code For retrieving the extras

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


Did you try debugging the extras? try putting break points and see what your are receiving in through the intent. Check if the extra is actually getting passed or not.

If you are not receiving the extra, then your intent is faulty. If you are receiving the extra, then the logic of how you access extras is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM