【问题标题】:How to clear notifications when the app is running?应用程序运行时如何清除通知?
【发布时间】:2018-06-18 04:27:32
【问题描述】:

我的应用根据 FirebaseDatabase 中的数据在我的服务类中创建一个通知。我想要完成的是清除应用程序打开时创建的通知。如果我单击通知本身,通知会自行清除,但我希望它在通过其他方式打开应用程序时也清除。此外,当我将数据添加到我的数据库时,即使应用程序正在运行,它也会创建一个通知。有没有办法禁用它?

这是我的服务

public class Service extends android.app.Service {

public DatabaseReference databaseReference, PantryReference, RefReference;
public ChildEventListener childEventListener, childEventListener1, childEventListener2;
public Date dateFormat;
public String mContentTitle = "NotifTest", mContentText;
private static final int uniqueID = 57891258;
public NotificationCompat.Builder notification;
private boolean mRunning;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public void onCreate() {
    super.onCreate();
    mRunning = false;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //---------------------- Check if its already running
    if (!mRunning){
        mRunning = true;

    //----------------------------------------------------------------------- Since Service cannot get groupUid in static from MainActivity
        SharedPreferences sharedPreferences = getSharedPreferences("GroupUid", Context.MODE_PRIVATE);
        String mGroupUid = sharedPreferences.getString("GroupUid", groupUid);

    //----------------------------------------------------------------------------------- Setting up Notification FreezerItems
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Groups").child(mGroupUid).child("HomeFragment").child("1");
        childEventListener = databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            HashMap<String, String> value = (HashMap<String, String>) dataSnapshot.getValue();
            if (value != null) {
                String name = value.get("name");
                String date = value.get("date");


                SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy", Locale.US);

                try {
                    dateFormat = sdf.parse(date);

                } catch (ParseException e) {
                    e.printStackTrace();
                }

                //------------------------------------------ setting the dates
                Calendar cal = Calendar.getInstance();
                cal.setTime(dateFormat);
                String mDate = sdf.format(cal.getTime());
                cal.add(Calendar.DATE, -1);
                String yesterday = sdf.format(cal.getTime());


                if (TodayDate.equals(yesterday)) {

                    mContentText =  name;
                    NotificationCreate(mContentText);

                } else if (TodayDate.equals(mDate)){

                    mContentText = name;
                    NotificationCreate(mContentText);
                }


            }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
    return super.onStartCommand(intent, flags, startId);
}


 private void createNotificationChannel(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "ChannelName";
        String description = "FoodExpiration";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("Default", name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}

【问题讨论】:

    标签: android notifications android-notifications


    【解决方案1】:

    下面是清除通知的最简单方法,您可以在需要的情况下使用它,只需记住您为清除特定通知而定义的通知 ID,否则您可以清除所有通知。

    具体通知

    public void clearNotofication(int notificationId) {
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager nMgr = (NotificationManager) this.getSystemService(ns);
            nMgr.cancel(notificationId);
        }
    

    全部取消

    只需将上面代码中的nMgr.cancel(notificationId); 替换为nMgr.cancelAll();

    【讨论】:

      【解决方案2】:

      用于清除单个通知:

      NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.cancel(NOTIFICATION_ID);
      

      用于清除所有通知。

      notificationManager.cancelAll();
      

      【讨论】:

        猜你喜欢
        • 2012-01-30
        • 2021-09-26
        • 2021-09-14
        • 2015-05-10
        • 2015-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-13
        相关资源
        最近更新 更多