如何在应用 运行 时清除通知?
How to clear notifications when the app is running?
我的应用根据 FirebaseDatabase 中的数据在我的服务 class 中创建通知。我想要完成的是清除应用程序打开时创建的通知。如果我点击通知本身,通知会自行清除,但我希望它在通过其他方式打开应用程序时也能清除。此外,当我将数据添加到我的数据库时,它会创建一个通知,即使该应用程序是 运行。有没有办法禁用它?
这是我的服务
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);
}
}
}
清除单个通知:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
并清除所有通知。
notificationManager.cancelAll();
最简单的清除通知的方法你可以在你需要的情况下使用它,只需要记住你为清除特定通知定义的通知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();
。
我的应用根据 FirebaseDatabase 中的数据在我的服务 class 中创建通知。我想要完成的是清除应用程序打开时创建的通知。如果我点击通知本身,通知会自行清除,但我希望它在通过其他方式打开应用程序时也能清除。此外,当我将数据添加到我的数据库时,它会创建一个通知,即使该应用程序是 运行。有没有办法禁用它?
这是我的服务
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);
}
}
}
清除单个通知:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
并清除所有通知。
notificationManager.cancelAll();
最简单的清除通知的方法你可以在你需要的情况下使用它,只需要记住你为清除特定通知定义的通知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();
。