2

I got a ValueEventListener

   query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                final Intent intent = new Intent(BackgroundService.this, NextActivity.class);
                Task<String> t=genrate();//not null

                        t.addOnCompleteListener(new OnCompleteListener<String>() {
                            @Override
                            public void onComplete(@NonNull Task<String> task) {
                                if (task.isSuccessful()) {
                                 intent.putExtra("token",task.getResult());

            }
        } });
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
                mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
                mBuilder.setVisibility(VISIBILITY_SECRET);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(BackgroundService.this);
                notificationManager.notify(121, mBuilder.build());

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

The token key is missing from NextActivity.

getIntent().getExtras().getString("token")

return null.

What could be the problem ?

RichardIV
  • 31
  • 6

4 Answers4

0

You need this flag: PendingIntent.FLAG_UPDATE_CURRENT in your PendingIntent
Change:

PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);

to:

PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
0

addValueEventListener() is asynchronous. You will get the result only inside onDataChange(). Move the code to show notification into a separate method and call it inside onDataChange().

query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final Intent intent = new Intent(BackgroundService.this, NextActivity.class);
                Task < String > t = genrate(); //not null

                t.addOnCompleteListener(new OnCompleteListener < String > () {
                    @Override
                    public void onComplete(@NonNull Task < String > task) {
                        if (task.isSuccessful()) {
                            intent.putExtra("token", task.getResult());

                            //Show notification here
                            showNotification(intent);
                        }
                    }
                });

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
});

Separate method to show notification.

private void showNotification(Intent intent) {
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
    mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
    mBuilder.setVisibility(VISIBILITY_SECRET);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(BackgroundService.this);
    notificationManager.notify(121, mBuilder.build());
}
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
0

Does your task.getResult() returns string? If yes, then try this in second activity.

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String result = extras.getString("token");                
    System.out.println("yeah"+result);
}
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
Ashish Kudale
  • 1,230
  • 1
  • 27
  • 51
-1

you are not calling startActivity(intent);

query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final Intent intent = new Intent(BackgroundService.this, NextActivity.class);
                Task < String > t = genrate(); //not null

                t.addOnCompleteListener(new OnCompleteListener < String > () {
                    @Override
                    public void onComplete(@NonNull Task < String > task) {
                        if (task.isSuccessful()) {
                            intent.putExtra("token", task.getResult());
                            startActivity(intent);
                        }
                    }
                });
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
                mBuilder.setContentIntent(pendingIntent).setAutoCancel(true);
                mBuilder.setVisibility(VISIBILITY_SECRET);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(BackgroundService.this);
                notificationManager.notify(121, mBuilder.build());

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
Erselan Khan
  • 692
  • 6
  • 13