Skip to content

Commit

Permalink
Add removeAllNotifications API to push notifications service (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
jperedadnr authored Jan 6, 2025
1 parent 85d16e7 commit b15d8a3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -159,4 +159,13 @@ static Optional<PushNotificationsService> 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();
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -96,6 +96,11 @@ public void setAppIconBadgeNumber(int badgeNumber) {
setApplicationIconBadgeNumber(badgeNumber);
}

@Override
public void removeAllNotifications() {
removeAllDeliveredNotifications();
}

private GoogleServicesConfiguration readGoogleServicesConfiguration() {
GoogleServicesConfiguration configuration = new GoogleServicesConfiguration();

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"));
Expand All @@ -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, "<init>", "(Landroid/app/Activity;)V");
jthrowable t = (*dalvikEnv)->ExceptionOccurred(dalvikEnv);
Expand Down Expand Up @@ -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 //
///////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,3 +40,5 @@
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

@end

void setBadgeCount(NSInteger badgeNumber);
30 changes: 28 additions & 2 deletions modules/push-notifications/src/main/native/ios/PushNotifications.m
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b15d8a3

Please sign in to comment.