我的应用根据FirebaseDatabase中的数据在我的Service类中创建通知。我想要完成的是清除应用程序打开时创建的通知。如果我点击通知本身,通知会自动清除,但我希望它也可以通过其他方式清除应用程序。此外,当我向我的数据库添加数据时,即使应用程序正在运行,它也会创建通知。有没有办法禁用它?
这是我的服务
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);
}
}
}
答案 0 :(得分:0)
清除单一Notificaion:
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
清除所有通知。
notificationManager.cancelAll();
答案 1 :(得分:0)
清除通知的最简单方法,您可以在需要的情况下使用它,只需要记住您为明确的特定通知定义的通知ID,否则您可以清除所有通知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();
。