自动删除 Firebase 通知
Auto Remove Firebase Notifications
我有一个问题,我已经阅读了 Make notification disappear after 5 minutes and Clearing notification after a few seconds,但我仍然不明白他们所说的删除通知的部分。我的 phone 收到一个传入的 firebase 通知,如果用户没有点击它,我希望在 20 秒后自动收到通知 removed/disappear。我可以知道如何实现它吗?
P.S 我也阅读了有关服务的内容。我是 Java 语言的新手,在尝试一个小演示时就学会了。 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);
}
根据我已经实施的创建新 class 和扩展 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);
是的,这很容易。
如果用户未阅读通知,则在您收到通知的地方添加一个处理程序,然后删除通知。
@Override
public void onMessageReceived(RemoteMessage message) {
sendNotification(message.getData().toString);
}
添加通知码
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);
}
取消通知代码。
private void removeNotification(int id) {
Handler handler = new Handler();
long delayInMilliseconds = 20000;
handler.postDelayed(new Runnable() {
public void run() {
notificationManager.cancel(id);
}
}, delayInMilliseconds);
}
我有一个问题,我已经阅读了 Make notification disappear after 5 minutes and Clearing notification after a few seconds,但我仍然不明白他们所说的删除通知的部分。我的 phone 收到一个传入的 firebase 通知,如果用户没有点击它,我希望在 20 秒后自动收到通知 removed/disappear。我可以知道如何实现它吗?
P.S 我也阅读了有关服务的内容。我是 Java 语言的新手,在尝试一个小演示时就学会了。 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);
}
根据我已经实施的创建新 class 和扩展 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);
是的,这很容易。 如果用户未阅读通知,则在您收到通知的地方添加一个处理程序,然后删除通知。
@Override
public void onMessageReceived(RemoteMessage message) {
sendNotification(message.getData().toString);
}
添加通知码
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);
}
取消通知代码。
private void removeNotification(int id) {
Handler handler = new Handler();
long delayInMilliseconds = 20000;
handler.postDelayed(new Runnable() {
public void run() {
notificationManager.cancel(id);
}
}, delayInMilliseconds);
}