Dismiss and auto dismiss for notifications
This commit is contained in:
parent
343494ece0
commit
aac0cfbb56
@ -50,6 +50,7 @@ public class MessagingService extends FirebaseMessagingService {
|
|||||||
private void sendNotification(Map<String, String> data) {
|
private void sendNotification(Map<String, String> data) {
|
||||||
String channelId, messageBody, messageTitle, imageUrl;
|
String channelId, messageBody, messageTitle, imageUrl;
|
||||||
String nTag;
|
String nTag;
|
||||||
|
boolean autoCancel;
|
||||||
if (!data.containsKey("channelId")) {
|
if (!data.containsKey("channelId")) {
|
||||||
channelId = "ha_notify";
|
channelId = "ha_notify";
|
||||||
} else {
|
} else {
|
||||||
@ -71,6 +72,28 @@ public class MessagingService extends FirebaseMessagingService {
|
|||||||
nTag = data.get("tag");
|
nTag = data.get("tag");
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Notification tag: " + nTag);
|
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");
|
imageUrl = data.get("image");
|
||||||
Intent intent = new Intent(this, MainActivity.class);
|
Intent intent = new Intent(this, MainActivity.class);
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
@ -82,7 +105,7 @@ public class MessagingService extends FirebaseMessagingService {
|
|||||||
.setSmallIcon(R.drawable.mini_icon)
|
.setSmallIcon(R.drawable.mini_icon)
|
||||||
.setContentTitle(messageTitle)
|
.setContentTitle(messageTitle)
|
||||||
.setContentText(messageBody)
|
.setContentText(messageBody)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(autoCancel)
|
||||||
.setSound(defaultSoundUri)
|
.setSound(defaultSoundUri)
|
||||||
.setContentIntent(pendingIntent);
|
.setContentIntent(pendingIntent);
|
||||||
if (URLUtil.isValidUrl(imageUrl)) {
|
if (URLUtil.isValidUrl(imageUrl)) {
|
||||||
@ -95,8 +118,9 @@ public class MessagingService extends FirebaseMessagingService {
|
|||||||
for (int i = 1; i <= 3; i++) {
|
for (int i = 1; i <= 3; i++) {
|
||||||
if (data.containsKey("action" + i)) {
|
if (data.containsKey("action" + i)) {
|
||||||
Intent broadcastIntent = new Intent(this, NotificationActionReceiver.class);
|
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("tag", nTag);
|
||||||
|
}
|
||||||
broadcastIntent.putExtra("actionData", data.get("action" + i + "_data"));
|
broadcastIntent.putExtra("actionData", data.get("action" + i + "_data"));
|
||||||
PendingIntent actionIntent = PendingIntent.getBroadcast(this, i, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
PendingIntent actionIntent = PendingIntent.getBroadcast(this, i, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||||
notificationBuilder.addAction(R.drawable.mini_icon, data.get("action" + i), actionIntent);
|
notificationBuilder.addAction(R.drawable.mini_icon, data.get("action" + i), actionIntent);
|
||||||
|
@ -20,11 +20,12 @@ public class NotificationActionReceiver extends BroadcastReceiver {
|
|||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String rawActionData = intent.getStringExtra("actionData");
|
String rawActionData = intent.getStringExtra("actionData");
|
||||||
String notificationTag = intent.getStringExtra("tag");
|
if (intent.hasExtra("tag")) {
|
||||||
Log.d(TAG, "Canceling notification by tag: " + notificationTag);
|
String notificationTag = intent.getStringExtra("tag");
|
||||||
Log.d(TAG, "action data: " + rawActionData);
|
Log.d(TAG, "Canceling notification by tag: " + notificationTag);
|
||||||
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
notificationManager.cancel(notificationTag, 0);
|
notificationManager.cancel(notificationTag, 0);
|
||||||
|
}
|
||||||
SharedPreferences prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE);
|
SharedPreferences prefs = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE);
|
||||||
String webhookId = prefs.getString("flutter.app-webhook-id", null);
|
String webhookId = prefs.getString("flutter.app-webhook-id", null);
|
||||||
if (webhookId != null) {
|
if (webhookId != null) {
|
||||||
|
Reference in New Issue
Block a user