我正在开发一个使用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);
}
}
答案 0 :(得分:0)
为实现此功能可以采取的解决方法:
使用Alarm Manager
并根据您的要求设置时间,并将广播接收器与之连接。时间到了,您的应用会收到回调[onReceive(Context context, Intent intent)
,您可以在其中使用通知ID取消通知。
使用NotificationManager从NotificationId获取通知。
对于通知ID,您可以存储它的首选项,或者在Db中存储,取决于您的要求。
希望这会对您有所帮助。