Make notification disappear after 5 minutes

2020-02-12 11:36发布

Is it possible to make a notification automatically disappear after a period of time?

标签: android
5条回答
贼婆χ
2楼-- · 2020-02-12 12:02

Yeah, it is very easy. Where you get notification there add one handler if notification is not read by user then remove notification.

@Override
public void onMessageReceived(RemoteMessage message) {
sendNotification(message.getData().toString);
}

add notification code

private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("TEST NOTIFICATION")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int id = 0;
        notificationManager.notify(id, notificationBuilder.build());
        removeNotification(id);
    } 

cancel notification code.

private void removeNotification(int id) {
Handler handler = new Handler();
    long delayInMilliseconds = 20000;
    handler.postDelayed(new Runnable() {
        public void run() {
            notificationManager.cancel(id);
        }
    }, delayInMilliseconds);
}
查看更多
Anthone
3楼-- · 2020-02-12 12:03
▲ chillily
4楼-- · 2020-02-12 12:07

You can use the AlarmManager. I think is more appropriate and more easier to implement than an Android Service.

With AlarmManager you do not need worry about make something running until the time finish. Android do that for you, and send a brodcast when it happen. Your application must have a Receiver to get the correct intent.

Look theses examples:

查看更多
闹够了就滚
5楼-- · 2020-02-12 12:12

You could also use a classic Java Runnable for a simple small Thread.

Handler h = new Handler();
    long delayInMilliseconds = 5000;
    h.postDelayed(new Runnable() {
        public void run() {
            mNotificationManager.cancel(id);
        }
    }, delayInMilliseconds);

Also look here:

Clearing notification after a few seconds

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-12 12:22

Yeah, you can just create a service that runs in the background that'll timeout after five minutes and delete your notification. Whether you "should" actually do that is up for debate. A notification should be there to notify the user... and the user should be able to dismiss it on their own.

From d.android.com:

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

查看更多
登录 后发表回答