diff --git a/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/PushNotificationsService.java b/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/PushNotificationsService.java index dc431575..9c405819 100644 --- a/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/PushNotificationsService.java +++ b/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/PushNotificationsService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2024, Gluon + * Copyright (c) 2016, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -159,4 +159,13 @@ static Optional create() { * or 0 to hide it. The default value is 0. */ void setAppIconBadgeNumber(int badgeNumber); + + /** + * Removes all delivered notifications that haven't been read yet. + * This can be used to remove pending notifications, when the user doesn't tap on + * the notification to open the app, but directly opens it. + * It also resets the badge number. + * @since 4.0.22 + */ + void removeAllNotifications(); } diff --git a/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/AndroidPushNotificationsService.java b/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/AndroidPushNotificationsService.java index 00a5dea8..1e6491c6 100644 --- a/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/AndroidPushNotificationsService.java +++ b/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/AndroidPushNotificationsService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2023, Gluon + * Copyright (c) 2016, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -96,6 +96,11 @@ public void setAppIconBadgeNumber(int badgeNumber) { setApplicationIconBadgeNumber(badgeNumber); } + @Override + public void removeAllNotifications() { + removeAllDeliveredNotifications(); + } + private GoogleServicesConfiguration readGoogleServicesConfiguration() { GoogleServicesConfiguration configuration = new GoogleServicesConfiguration(); @@ -153,6 +158,7 @@ private GoogleServicesConfiguration readGoogleServicesConfiguration() { private native String getErrorString(int resultCode); private native void initializeFirebase(String applicationId, String projectNumber, String projectId, String apiKey); private native void setApplicationIconBadgeNumber(int badgeNumber); + private native void removeAllDeliveredNotifications(); // callback private static void setToken(String token) { diff --git a/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/IOSPushNotificationsService.java b/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/IOSPushNotificationsService.java index 338c9064..a8258843 100644 --- a/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/IOSPushNotificationsService.java +++ b/modules/push-notifications/src/main/java/com/gluonhq/attach/pushnotifications/impl/IOSPushNotificationsService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2023, Gluon + * Copyright (c) 2016, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -80,9 +80,15 @@ public void setAppIconBadgeNumber(int badgeNumber) { setApplicationIconBadgeNumber(badgeNumber); } + @Override + public void removeAllNotifications() { + removeAllDeliveredNotifications(); + } + // native private static native void initPushNotifications(); private static native void setApplicationIconBadgeNumber(int badgeNumber); + private static native void removeAllDeliveredNotifications(); /** * @param s String with the error description diff --git a/modules/push-notifications/src/main/native/android/c/pushnotifications.c b/modules/push-notifications/src/main/native/android/c/pushnotifications.c index 1bf65591..0ec4623f 100644 --- a/modules/push-notifications/src/main/native/android/c/pushnotifications.c +++ b/modules/push-notifications/src/main/native/android/c/pushnotifications.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023, Gluon + * Copyright (c) 2020, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,6 +39,7 @@ jmethodID jDalvikPushNotificationsServiceIsGooglePlayServicesAvailable; jmethodID jDalvikPushNotificationsServiceGetErrorString; jmethodID jDalvikPushNotificationsServiceInitializeFirebase; jmethodID jDalvikPushNotificationsServiceSetBadgeNumber; +jmethodID jDalvikPushNotificationsServiceRemoveAllNotifications; static void initializeGraalHandles(JNIEnv* env) { jGraalPushNotificationsClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/attach/pushnotifications/impl/AndroidPushNotificationsService")); @@ -56,6 +57,7 @@ static void initializePushNotificationsDalvikHandles() { jDalvikPushNotificationsServiceGetErrorString = (*dalvikEnv)->GetMethodID(dalvikEnv, jPushNotificationsServiceClass, "getErrorString", "(I)Ljava/lang/String;"); jDalvikPushNotificationsServiceInitializeFirebase = (*dalvikEnv)->GetMethodID(dalvikEnv, jPushNotificationsServiceClass, "initializeFirebase", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); jDalvikPushNotificationsServiceSetBadgeNumber = (*dalvikEnv)->GetMethodID(dalvikEnv, jPushNotificationsServiceClass, "setBadgeNumber", "(I)V"); + jDalvikPushNotificationsServiceRemoveAllNotifications = (*dalvikEnv)->GetMethodID(dalvikEnv, jPushNotificationsServiceClass, "removeAllNotifications", "()V"); jmethodID jPushNotificationsServiceInitMethod = (*dalvikEnv)->GetMethodID(dalvikEnv, jPushNotificationsServiceClass, "", "(Landroid/app/Activity;)V"); jthrowable t = (*dalvikEnv)->ExceptionOccurred(dalvikEnv); @@ -167,6 +169,17 @@ JNIEXPORT void JNICALL Java_com_gluonhq_attach_pushnotifications_impl_AndroidPus DETACH_DALVIK(); } +JNIEXPORT void JNICALL Java_com_gluonhq_attach_pushnotifications_impl_AndroidPushNotificationsService_removeAllDeliveredNotifications +(JNIEnv *env, jclass jClass) +{ + if (isDebugAttach()) { + ATTACH_LOG_FINE("Remove all delivered notifications"); + } + ATTACH_DALVIK(); + (*dalvikEnv)->CallVoidMethod(dalvikEnv, jDalvikPushNotificationsService, jDalvikPushNotificationsServiceRemoveAllNotifications); + DETACH_DALVIK(); +} + /////////////////////////// // From Dalvik to native // /////////////////////////// diff --git a/modules/push-notifications/src/main/native/android/dalvik/DalvikPushNotificationsService.java b/modules/push-notifications/src/main/native/android/dalvik/DalvikPushNotificationsService.java index 17afe09c..228ec5fe 100644 --- a/modules/push-notifications/src/main/native/android/dalvik/DalvikPushNotificationsService.java +++ b/modules/push-notifications/src/main/native/android/dalvik/DalvikPushNotificationsService.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2024, Gluon + * Copyright (c) 2016, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,6 +29,7 @@ import android.Manifest; import android.app.Activity; +import android.app.NotificationManager; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; @@ -152,6 +153,12 @@ public void setBadgeNumber(int number) { DalvikPushNotificationsService.badgeNumber = number; } + public void removeAllNotifications() { + NotificationManager notificationManager = + (NotificationManager) activity.getSystemService(Activity.NOTIFICATION_SERVICE); + notificationManager.cancelAll(); + } + static void sendRuntimeArgs(String value) { processRuntimeArgs(LAUNCH_PUSH_NOTIFICATION_KEY, value); } diff --git a/modules/push-notifications/src/main/native/ios/PushNotifications.h b/modules/push-notifications/src/main/native/ios/PushNotifications.h index 0cff2c8b..bbfa3e60 100644 --- a/modules/push-notifications/src/main/native/ios/PushNotifications.h +++ b/modules/push-notifications/src/main/native/ios/PushNotifications.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019, Gluon + * Copyright (c) 2016, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,3 +40,5 @@ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; @end + +void setBadgeCount(NSInteger badgeNumber); \ No newline at end of file diff --git a/modules/push-notifications/src/main/native/ios/PushNotifications.m b/modules/push-notifications/src/main/native/ios/PushNotifications.m index 96ceddfe..e91bf486 100644 --- a/modules/push-notifications/src/main/native/ios/PushNotifications.m +++ b/modules/push-notifications/src/main/native/ios/PushNotifications.m @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2023, Gluon + * Copyright (c) 2016, 2025, Gluon * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -108,7 +108,33 @@ if (debugAttach) { AttachLog(@"Set badge number to %d", badgeNumber); } - [UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber; + setBadgeCount(badgeNumber); +} + +JNIEXPORT void JNICALL Java_com_gluonhq_attach_pushnotifications_impl_IOSPushNotificationsService_removeAllDeliveredNotifications +(JNIEnv *env, jclass jClass) +{ + if (debugAttach) { + AttachLog(@"Remove all delivered notifications"); + } + if (@available(iOS 10.0, *)) + { + [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications]; + } else + { + [[UIApplication sharedApplication] cancelAllLocalNotifications]; + } + setBadgeCount(0); +} + +void setBadgeCount(NSInteger badgeNumber) { + if (@available(iOS 16.0, *)) + { + [[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:badgeNumber withCompletionHandler:nil]; + } else + { + [UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber; + } } @implementation GlassApplication (NotificationsAdditions)