android - 使用基于火的推送通知清除系统托盘上的通知
问题描述
我正在开发一个使用 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);
}
}
解决方案
您可以采取的解决方法来实现此功能:
根据您的要求使用Alarm Manager
和设置它的时间,并附上广播接收器。当时间到达您的应用程序会收到回调[ onReceive(Context context, Intent intent)
],您可以使用通知 ID 取消通知。
使用 NotificationManager 从 NotificationId 获取通知。
对于通知 ID,您可以将其存储偏好或在 Db 中,这取决于您的要求,您有多少 id。
希望这会帮助你。
推荐阅读
- java - 使用获取请求发送查询?
- python - 尝试 BeautifulSoup 抓取时被网站阻止
- php - 如何在 PHP 中创建自定义 SOAP 标头值?
- sql - ER_OPERAND_COLUMNS:操作数应包含 1 列
- java - 如何使用变量在 Java 中查询 MongoDB
- python - 如何从用户输入中解析逗号分隔的整数
- c# - JWT:删除用户
- npm - 如何从我的 Laravel 应用程序中撤消 Inertia.js
- google-calendar-api - 如何使用 C# 使用参数或其他方式将会议数据版本添加到事件
- c - TCP 客户端连接到两个 TCP 服务器