android - 当项目添加到 recyclerview 时收到通知

标签 android android-studio android-recyclerview notifications

当项目添加到 recyclerview 时如何接收通知?

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

    final OrdersData order = mUsers.get(position);
    final String client_Id = order.getClient_id();
    title = order.getItems();
    text = order.getF_name();
    holder.title.setText(order.getItems());
    holder.subtitle.setText(order.getF_name());
    holder.timeStamp.setText(order.getDateStamp());
    holder.address.setText(order.getAddress());
    String no = String.valueOf(position);
    holder.orderNo.setText(no);

}

最佳答案

您可以通过多种方式构建本地通知。对于您的情况,我会推荐最简单的方法。只需在将数据添加到 RecyclerView 之前调用 buildLocalNotification() 函数,然后通知 RecyclerView 中更改的数据。

  private void buildLocalNotification(String title, String message) {

        Intent intent = new Intent(getApplicationContext(), BaseActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_push_notification)
                        .setSound(defaultSoundUri)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        /**
         * Since Android Oreo
         */

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            createNotificationChannel(channelId, notificationManager);

        }

        assert notificationManager != null;
        notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build());
    }

    @SuppressLint("NewApi")
    public void createNotificationChannel(String channelId, NotificationManager notificationManager) {


        @SuppressLint("WrongConstant")
        NotificationChannel channel = new NotificationChannel(channelId, getString(R.string.app_name),
                NotificationManager.IMPORTANCE_MAX);
        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build();
        assert notificationManager != null;
        if (AppConstants.DEFAULT_NOTIFICATION_SOUND_URI != null) {
            channel.setSound(AppConstants.DEFAULT_NOTIFICATION_SOUND_URI, att);
        }
        channel.setLightColor(Color.parseColor("#F1E605"));
        channel.setVibrationPattern(VIBRATE_PATTERN);
        channel.canShowBadge();
        channel.enableVibration(true);
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);

    }

关于android - 当项目添加到 recyclerview 时收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58725483/

相关文章:

android - 单击任何按钮刷新选项卡布局中的 fragment

android - 在键盘上显示 "Done"

java - android应用程序 Activity 中的保存按钮?

Android 回收查看 LogCat : Could not find method

android - 获取屏幕上的位置以便在 View 持有者中查看

java - 关闭应用程序直到特定日期

java - 如何以正确/列出的顺序获取 JSONObject 值?

android - ARCore – 在我面前渲染的 3D 对象

java - 我无法应用 gradle 6.x 插件

firebase - 无法通过 `CompileOptions.compilerArgs` 指定 -processorpath 或 --processor-path。请改用 `CompileOptions.annotationProcessorPath` 属性