java - 如何从NotificationAdapter类android studio推送通知

标签 java android notifications

我已经设置了我的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) {

            }
        });
    }
}

对于我想要的结果,当用户退出应用程序并且他的帖子被喜欢时,他应该收到通知,此人喜欢您的这篇帖子,并通过单击该通知,他重定向到特定帖子。

最佳答案

如果您希望您的用户在应用程序处于后台或关闭时收到通知(如您所说),请实现一个服务组件并从那里启动您的通知。

即使您的应用程序在后台,服务也会继续运行

关于java - 如何从NotificationAdapter类android studio推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57122616/

相关文章:

java - [Microsoft][ODBC 驱动程序管理器] 无效字符串或缓冲区长度异常

java赋值正则表达式

android - 运行 google map v2 应用程序时出现异常

Android 通过 Google Play 推送通知

ios - iOS 中的本地通知是否需要开发者帐户?

java - 关闭应用程序后服务未运行

java - 自定义列表重复最后添加的元素

java - XMLBeam 的默认值

android - 如何在 App Brain Market 中发布我的应用程序?

android - 有什么方法可以使运行 android 4.0 的 webview 的性能达到可接受的水平?