سؤال

أنا أعرض ملف Notification في شريط الحالة اعتمادًا على حالتي. حتى هذا ، لا بأس.

المشكلة الآن في طلبي هي عندما أعود إلى التطبيق ، لا يزال Notification يتم عرضه في شريط الحالة.

لا أريد Notification عندما أعود من التطبيق. لهذا أعطني بعض الاقتراحات.

هل كانت مفيدة؟

المحلول

تستطيع cancel() بنفسك Notifications عبر NotificationManager. الأمر متروك لك لتقرير متى cancel() ومتى لإظهاره.

نصائح أخرى

ليس لدي ما يكفي من مندوب لإضافة تعليق SoCommonware. . . سيكون من المفيد للقراء المستقبليين أكثر إذا أعطيت المزيد من التفاصيل المحددة مثل إخبارهم باستخدام أ "معرف الإخطار" عندما يقومون بإنشاء الإخطار واستخدام هذا المعرف لإلغاء الإخطار. إذا أعطيت إجابة ، اجعلها كاملة أو على الأقل توفر التفاصيل الكاملة. هذه المنشورات لا تهدف فقط إلى OP ، بل إنها تساعد الآخرين الذين صادفوا هذا المنشور أيضًا. مثل ذلك

final NotificationManager notificationManager = (NotificationManager) getSystemService (NOTIFICATION_SERVICE);

final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());

notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

ثم لإلغاءها تتصل بها

notificationManager.cancel(NOTIFICATION_ID);

أو يمكنك الاتصال

notificationManager.cancelAll();
notificationManager.cancelAll();
private void cancelNotificationInBar() {
        NotificationCreator notification = new NotificationCreator(getApplicationContext());
        notification.mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        if (notification.mNotificationManager != null) {
            notification.mNotificationManager.cancelAll();
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top