通知应在满足情况后停止

发布于 2025-02-11 13:11:59 字数 5203 浏览 1 评论 0原文

如果数组中有一些数据,则该应用程序将通知每分钟,但是一旦数组为空,通知应停止,但不会停止。我的数组中的数据来自Firebase实时数据库。

这是我的代码。它在 addChildEventListener 内部。

Listeth 是数组。我可以很好地调用数据库中的数据。

if (listEth.contains(null)){
     // it will do nothing if there is no data
} else {
   NotifyUsers();      // notify every 24 hours
   notification();     // 
}

这是针对我的 AlarmManager

public final void NotifyUsers(){
        Intent intent = new Intent(this, NotifyPendingAssignments.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                2, intent, PendingIntent.FLAG_IMMUTABLE);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                60000, pendingIntent);  //set repeating every 1 min
    }

这是数据的方式,

databaseEth.addValueEventListener(new ValueEventListener() {
     @SuppressLint("NotifyDataSetChanged")
     @Override
     public void onDataChange(@NonNull DataSnapshot snapshot) {
         for(DataSnapshot dataSnapshot : snapshot.getChildren()){ 
             assignmentEth assignmentEth = dataSnapshot.getValue
                  (com.example.timemanagementapp.assignmentEth.class);
             listEth.add(assignmentEth);
         }
         myAdapterEth.notifyDataSetChanged();
     }

     @Override
        public void onCancelled(@NonNull DatabaseError error) {
     }
});

我尝试添加另一个通知频道,该通知通道将在用户打开时不断通知,并在数组为空时停止。有用。

private void notification() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel =
                    new NotificationChannel("eth", "eth", NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "eth")
                    .setContentText("New Assignment")
                    .setSmallIcon(R.drawable.ic_baseline_notifications)
                    .setAutoCancel(true)
                    .setContentText("You have new assignment in ETH");

            NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
            managerCompat.notify(120, builder.build());
        }
    }

但是我需要的是向广播castreceiver (notifyusers())通知,即使应用程序不打开,它也会通知

这是我的 createNotificationChannel()

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

            CharSequence name = "notifyPendingAssignment";
            String description = "You have pending assignments in ETH";

            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("notifyPendingAssignment", name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

and> and> and>和这是我的 createNotificationChannel()的广播员

public class NotifyPendingAssignments extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyPendingAssignment")
                .setSmallIcon(R.drawable.icon_notif)
                .setContentTitle("Pending Assignment")
                .setContentText("You still have pending assignments.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(202, builder.build());
    }
}

,这是我的 notifyuser()

public final void NotifyUsers(){
        Intent intent = new Intent(this, NotifyPendingAssignments.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                2, intent, PendingIntent.FLAG_IMMUTABLE);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                60000, pendingIntent);  //set repeating every 1 min
    }

The app will notify every minute if there are some data in the Array, but once the array is empty, the notification should stop but it does not. The data inside my array are derived from firebase realtime database.

This is my code for that. it is inside addChildEventListener.

listEth is the array. I can call the data in the database just fine.

if (listEth.contains(null)){
     // it will do nothing if there is no data
} else {
   NotifyUsers();      // notify every 24 hours
   notification();     // 
}

This is for my AlarmManager

public final void NotifyUsers(){
        Intent intent = new Intent(this, NotifyPendingAssignments.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                2, intent, PendingIntent.FLAG_IMMUTABLE);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                60000, pendingIntent);  //set repeating every 1 min
    }

This is how the data is called

databaseEth.addValueEventListener(new ValueEventListener() {
     @SuppressLint("NotifyDataSetChanged")
     @Override
     public void onDataChange(@NonNull DataSnapshot snapshot) {
         for(DataSnapshot dataSnapshot : snapshot.getChildren()){ 
             assignmentEth assignmentEth = dataSnapshot.getValue
                  (com.example.timemanagementapp.assignmentEth.class);
             listEth.add(assignmentEth);
         }
         myAdapterEth.notifyDataSetChanged();
     }

     @Override
        public void onCancelled(@NonNull DatabaseError error) {
     }
});

I tried adding another notification channel that will continuously notify when the user opens it, and stops when the array is empty. It works.

private void notification() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel =
                    new NotificationChannel("eth", "eth", NotificationManager.IMPORTANCE_DEFAULT);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "eth")
                    .setContentText("New Assignment")
                    .setSmallIcon(R.drawable.ic_baseline_notifications)
                    .setAutoCancel(true)
                    .setContentText("You have new assignment in ETH");

            NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
            managerCompat.notify(120, builder.build());
        }
    }

But what I need is the notification with the BroadcastReceiver (NotifyUsers()) so that it will notify even if the app is not open

This is my code for CreateNotificationChannel()

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

            CharSequence name = "notifyPendingAssignment";
            String description = "You have pending assignments in ETH";

            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("notifyPendingAssignment", name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

and this is my BroadcastReceiver for CreateNotificationChannel()

public class NotifyPendingAssignments extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyPendingAssignment")
                .setSmallIcon(R.drawable.icon_notif)
                .setContentTitle("Pending Assignment")
                .setContentText("You still have pending assignments.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(202, builder.build());
    }
}

and this is my NotifyUser()

public final void NotifyUsers(){
        Intent intent = new Intent(this, NotifyPendingAssignments.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                2, intent, PendingIntent.FLAG_IMMUTABLE);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                60000, pendingIntent);  //set repeating every 1 min
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2025-02-18 13:11:59

我通过使用 notifyuser()移动if其他条件并在该条件内设置AlarmManager来解决此问题。

if (listEth.isEmpty() || listEth.contains(null)){
      Toast.makeText(AssignmentListEth.this, "There is no Pending Assignment",
            Toast.LENGTH_SHORT).show();
      alarmManager.cancel(pendingIntent);
      pendingIntent.cancel();
} else {
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    60000, pendingIntent);  // every 1 minute
}

I solved this issue by moving the if else condition with NotifyUser() and setting the AlarmManager inside that condition.

if (listEth.isEmpty() || listEth.contains(null)){
      Toast.makeText(AssignmentListEth.this, "There is no Pending Assignment",
            Toast.LENGTH_SHORT).show();
      alarmManager.cancel(pendingIntent);
      pendingIntent.cancel();
} else {
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    60000, pendingIntent);  // every 1 minute
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文