Dismiss and auto dismiss for notifications

This commit is contained in:
Yegor Vialov 2020-05-25 10:20:48 +00:00
parent 343494ece0
commit aac0cfbb56
2 changed files with 33 additions and 8 deletions

View File

@ -50,6 +50,7 @@ public class MessagingService extends FirebaseMessagingService {
private void sendNotification(Map<String, String> data) {
String channelId, messageBody, messageTitle, imageUrl;
String nTag;
boolean autoCancel;
if (!data.containsKey("channelId")) {
channelId = "ha_notify";
} else {
@ -71,6 +72,28 @@ public class MessagingService extends FirebaseMessagingService {
nTag = data.get("tag");
}
Log.d(TAG, "Notification tag: " + nTag);
if (data.containsKey("dismiss")) {
try {
boolean dismiss = Boolean.parseBoolean(data.get("dismiss"));
if (dismiss) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(nTag, 0);
return;
}
} catch (Exception e) {
//nope
}
}
if (data.containsKey("autoDismiss")) {
try {
autoCancel = Boolean.parseBoolean(data.get("autoDismiss"));
} catch (Exception e) {
autoCancel = true;
}
} else {
autoCancel = true;
}
imageUrl = data.get("image");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
@ -82,7 +105,7 @@ public class MessagingService extends FirebaseMessagingService {
.setSmallIcon(R.drawable.mini_icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setAutoCancel(autoCancel)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (URLUtil.isValidUrl(imageUrl)) {
@ -95,8 +118,9 @@ public class MessagingService extends FirebaseMessagingService {
for (int i = 1; i <= 3; i++) {
if (data.containsKey("action" + i)) {
Intent broadcastIntent = new Intent(this, NotificationActionReceiver.class);
Log.d(TAG, "Putting a tag to the action: " + nTag);
if (autoCancel) {
broadcastIntent.putExtra("tag", nTag);
}
broadcastIntent.putExtra("actionData", data.get("action" + i + "_data"));
PendingIntent actionIntent = PendingIntent.getBroadcast(this, i, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.addAction(R.drawable.mini_icon, data.get("action" + i), actionIntent);

View File

@ -20,11 +20,12 @@ public class NotificationActionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String rawActionData = intent.getStringExtra("actionData");
if (intent.hasExtra("tag")) {
String notificationTag = intent.getStringExtra("tag");
Log.d(TAG, "Canceling notification by tag: " + notificationTag);
Log.d(TAG, "action data: " + rawActionData);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationTag, 0);
}
SharedPreferences prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE);
String webhookId = prefs.getString("flutter.app-webhook-id", null);
if (webhookId != null) {