我正在开发一个使用 Firebase Cloud Messaging 的应用程序。但是我有一种情况,应用程序已关闭,处于终止状态,并且用户在 10 秒后未单击通知栏如何自动关闭此通知。我的问题是:当应用程序处于终止状态并且通知位于系统托盘中时,如何自动关闭此通知。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String ChannelID = "Qwykr";
public static final int NOTIFICATION_ID = 1041;
NotificationManager notificationManager;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String RiderLOcation = remoteMessage.getData().get("RiderLocation");
String RiderID = remoteMessage.getData().get("RiderID");
String Dlat = remoteMessage.getData().get("Dlat");
String DLong = remoteMessage.getData().get("DLong");
String Plat = remoteMessage.getData().get("Plat");
String Plong = remoteMessage.getData().get("Plong");
String RiderName = remoteMessage.getData().get("RiderName");
String MainBody="RiderID:"+RiderID+";RiderName:"+RiderName+";RiderLocation:"
+RiderLOcation+";PLat:"+Plat+";PLong:"+Plong+";DLat:"+Dlat+";DLong:"+DLong;
System.out.println("notificationIntentIs On Recevied"+MainBody);
sendNotification(MainBody);
PreferenceHandler.writeString(this, PreferenceHandler.RIDE_DETAIL,MainBody);
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("bookingId", messageBody);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
startActivity(intent);
Intent YesButton = new Intent(this, MainActivity.class);
Bundle bundle1 = new Bundle();
bundle1.putString("bookingId", messageBody);
YesButton.putExtras(bundle1);
YesButton.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK|
Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent YesPendingIntent = PendingIntent.getActivity(this, 0,
YesButton,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.BLUE, 100, 3000)
.setPriority(Notification.DEFAULT_ALL)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getString(R.string.notificationData))
.setSound(defaultSoundUri)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(NOTIFICATION_ID /* ID of notification */,
notificationBuilder.build());
clearNotification(NOTIFICATION_ID);
}
public void clearNotification(int ID) {
Handler handler=new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
}
};
Runnable doDisplayError = new Runnable() {
public void run() {
notificationManager.cancel(ID);
}
};
handler.postDelayed(doDisplayError,3000);
}
}