我有用户列表,当我发送消息时,该组中的所有用户都收到消息,但未发送通知。我使用for each循环遍历用户列表并发送通知。我正在使用相同的代码从一个用户向另一个用户发送通知(只需稍作修改)。请问我做错了什么?下面是我的代码:
// sends notification to respective users as soon as message is sent
private void sendNotification(final List<String> receivers, final String username , final String message){
/* for loop to loop through the list of users id in
the group and send the notification accordingly */
for(final String id : receivers){
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference(Constants.TOKENS_REF);
Query query = tokens.orderByKey().equalTo(id);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Token token = snapshot.getValue(Token.class);
Data data = new Data(admin_uid, R.mipmap.app_logo_round, username+": "+message,
getString(R.string.application_name), id);
assert token != null;
Sender sender = new Sender(data, token.getToken());
// apiService object to sendNotification to users
apiService.sendNotification(sender)
.enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if(response.code() == 200){
if(response.body().success != 1){
Toast.makeText(GroupMessageActivity.this,"Failed!",Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
// display error message
Toast.makeText(GroupMessageActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// display error message
Toast.makeText(GroupMessageActivity.this,databaseError.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:0)
我找到了答案,那就是通过“清理我的项目”和“重建项目”(为此,在菜单栏上找到“生成”,单击它,您将看到“清理项目”和“重建项目”。请单击“清理项目”,耐心等待gradle完成,现在单击Rebuild project),它为我工作...我上面发布的代码工作正常,但我认为android studio行为异常。希望它有助于防止有人遇到类似问题。