我有一个问题,我都读过让通知在 5 分钟后消失 https://stackoverflow.com/questions/15648699/make-notification-disappear-after-5-minutes and 几秒钟后清除通知 https://stackoverflow.com/questions/4994126/clearing-notification-after-a-few-seconds,但我仍然不明白他们所说的删除通知的部分。我的手机收到了一个传入的 firebase 通知,如果用户没有点击它,我希望该通知在 20 秒后自动删除/消失。我可以知道如何实施吗?
P.S 我也阅读了有关服务的内容。我对 Java 语言很陌生,在尝试一些演示时就开始使用它。https://developer.android.com/guide/components/services.html https://developer.android.com/guide/components/services.html
每次我收到通知时,下面的代码都会使我的应用程序崩溃。任何帮助将不胜感激。
编辑:它的功能齐全。供大家参考
private void removeNotification()
{
long delayInMilliseconds = 20000;
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run()
{
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(0);
timer.cancel();
}
}, delayInMilliseconds, 1000);
}
按照我已经实现的创建新类并扩展 FirebaseMessagingService
您可以编写以下代码来发送通知:
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);
notificationManager.notify(0, notificationBuilder.build());
}
对于取消通知创建新方法并编写以下代码:
Handler h = new Handler();
long delayInMilliseconds = 20000;
h.postDelayed(new Runnable() {
public void run() {
notificationManager.cancel(id);
}
}, delayInMilliseconds);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)