我已经设置了我的notifiactionAdapter类,如下所示,它可以正常工作,即当帖子收到像注释或我在此notificationAdapter类控制的通知位置中得到通知的任何内容时。无论用户是否使用应用,我都希望将这些信息作为通知推送。
我试图在该notificationAdapter.java中构建NotificationCompat.builder,但是我无法从其中获取图标,标题,正文等数据。
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ImageViewHolder> {
private Context mContext;
private List < Notification > mNotification;
public NotificationAdapter (Context context, List<Notification> notification){
mContext = context;
mNotification = notification;
}
@NonNull
@Override
public NotificationAdapter . ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater . from (mContext).inflate(R.layout.notification_item, parent, false);
return new NotificationAdapter . ImageViewHolder (view);
}
@Override
public void onBindViewHolder(@NonNull final NotificationAdapter . ImageViewHolder holder, final int position) {
final Notification notification = mNotification.get(position);
holder.text.setText(notification.getText());
getUserInfo(holder.image_profile, holder.username, notification.getUserid());
if (notification.isIspost()) {
holder.post_image.setVisibility(View.VISIBLE);
getPostImage(holder.post_image, notification.getPostid());
} else {
holder.post_image.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(new View . OnClickListener () {
@Override
public void onClick(View view) {
if (notification.isIspost()) {
SharedPreferences.Editor editor = mContext . getSharedPreferences ("PREFS", MODE_PRIVATE).edit();
editor.putString("postid", notification.getPostid());
editor.apply();
((FragmentActivity) mContext).getSupportFragmentManager().beginTransaction().replace(
R.id.fragment_container,
new PostDetailFragment ()
).commit();
} else {
SharedPreferences.Editor editor = mContext . getSharedPreferences ("PREFS", MODE_PRIVATE).edit();
editor.putString("profileid", notification.getUserid());
editor.apply();
((FragmentActivity) mContext).getSupportFragmentManager().beginTransaction().replace(
R.id.fragment_container,
new ProfileFragment ()
).commit();
}
}
});
}
//
@Override
public int getItemCount() {
return mNotification.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public ImageView image_profile, post_image;
public TextView username, text;
public ImageViewHolder (View itemView) {
super(itemView);
image_profile = itemView.findViewById(R.id.image_profile);
post_image = itemView.findViewById(R.id.post_image);
username = itemView.findViewById(R.id.username);
text = itemView.findViewById(R.id.comment);
}
}
private void getUserInfo(final ImageView imageView, final TextView username, String publisherid) {
DatabaseReference reference = FirebaseDatabase . getInstance ().getReference()
.child("Users").child(publisherid);
reference.addListenerForSingleValueEvent(new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot . getValue (User.class);
Glide.with(mContext).load(user.getImageurl()).into(imageView);
username.setText(user.getUsername());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getPostImage(final ImageView post_image, String postid) {
DatabaseReference reference = FirebaseDatabase . getInstance ().getReference()
.child("Posts").child(postid);
reference.addListenerForSingleValueEvent(new ValueEventListener () {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Post post = dataSnapshot . getValue (Post.class);
Glide.with(mContext).load(post.getPostimage()).into(post_image);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
对于我想要的结果,当用户退出应用程序并且他的帖子被喜欢时,他应该得到通知,该人喜欢你的帖子,并单击该通知,他将重定向到特定帖子。
答案 0 :(得分:0)
如果您希望您的用户在APP处于后台或关闭状态时收到通知,请实施服务组件并从那里启动通知。
即使您的APP处于后台,服务也将继续运行