首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在应用程序运行时清除通知?

如何在应用程序运行时清除通知?
EN

Stack Overflow用户
提问于 2018-06-18 04:27:32
回答 2查看 2.1K关注 0票数 0

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

这是我的服务

代码语言:javascript
运行
复制
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);
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-18 04:37:44

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

专用通知

代码语言:javascript
运行
复制
public void clearNotofication(int notificationId) {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) this.getSystemService(ns);
        nMgr.cancel(notificationId);
    }

取消所有

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

票数 1
EN

Stack Overflow用户

发布于 2018-06-18 04:29:34

用于清除单个Notificaion:

代码语言:javascript
运行
复制
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

以及清除所有通知。

代码语言:javascript
运行
复制
notificationManager.cancelAll();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50902677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档