我有一个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) {
}
});
token
中缺少NextActivity
键。
getIntent().getExtras().getString("token")
返回null
。
可能是什么问题?
答案 0 :(得分:0)
您需要以下标记:PendingIntent.FLAG_UPDATE_CURRENT
中的PendingIntent
更改:
PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, 0);
至:
PendingIntent pendingIntent = PendingIntent.getActivity(BackgroundService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
答案 1 :(得分:0)
addValueEventListener()
是异步的。您只会在onDataChange()
内部获得结果。将代码显示通知的代码移到单独的方法中,然后在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) {
}
});
});
显示通知的单独方法。
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());
}
答案 2 :(得分:0)
您的task.getResult()
返回字符串吗?如果是,请在第二个活动中尝试。
Bundle extras = getIntent().getExtras();
if (extras != null) {
String result = extras.getString("token");
System.out.println("yeah"+result);
}
答案 3 :(得分:-1)
您没有打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) {
}
});