Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Remotely clear all notifications #2372

Closed
wants to merge 3 commits into from

Conversation

Moghul
Copy link

@Moghul Moghul commented May 17, 2018

Added handling for a "clearAllNotifications": "true" key-value pair on iOS and Android which clears the notifications.

Description

Simply added code which gets the value from the payload, and if it's "true", it executes code which removes notifications from the notification area.

Related Issue

#2371

Motivation and Context

It's a straight-forward way to clear time-sensitive notifications an app may have. Simply send the payload, and it's done.

How Has This Been Tested?

Android 6.0.1 and iOS 11.3.1
Notifications sent via Postman

Sent notifications to both platforms, both in background and force-closed. Then, sent the payload which clears the notififications.

Android
Background: Notifications all disappear.
Force-closed: Notifications all disappear.

iOS
Background: Notifications all disappear
Force-closed: Nothing happens. Doesn't seem to be able to run the code. Would love some help with this if you want.

iOS note : Why does the iOS code first increment the iconApplicationBadgeNumber and then clear it before removing the notifications?
Simply put, it just didn't work otherwise. If you know better, let me know.

Notification used to clear the pre-existing notifications:

{
    "priority" : "high",
    "content_available" : true,
    "data" : {
      "notId": 0,
      "clearAllNotifications" : "true"
    },
    "to" : "id"
}

In the case that you send a notification with a body and a title, and it also contains this payload (see what I mean below), first, the old notifications will be cleared, and then the new notification will be added. This will result in one remaining notification in the notification drawer.

Android:

{
    "priority" : "high",
    "content_available" : true,
    "data" : {
      "title": "some title or another",
      "body": "some body or another",
      "notId": 0,
      "clearAllNotifications" : "true"
    },
    "to" : "id"
}

iOS:

{
    "priority" : "high",
    "content_available" : true,
    "notification": {
         "title": "some title or another",
         "body": "some body or another",  
    },
    "data" : {
      "notId": 0,
      "clearAllNotifications" : "true"
    },
    "to" : "id"
}

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@fredgalvao
Copy link
Collaborator

One point of possible confusion I see is the fact that the handling of the new attribute doesn't define an independent top level behavior, but instead define a new "minor feature", parallel to the usual notification handling. In other terms, you mentioned this in the issue, but we need to make sure this is explicit and clean in the docs, at the very least:

In the case that you send a notification with a body and a title, and it also contains this payload (see what I mean below), first, the old notifications will be cleared, and then the new notification will be added. This will result in one remaining notification in the notification drawer.

Instead of calling this "a notification payload do clear the drawer", we should call it "a normal notification, that can also explicitly ask to clear the drawer before being handled".

I hope I made myself clear, words are hard.

@Moghul
Copy link
Author

Moghul commented May 18, 2018

I understand what you mean. I'll try and whip up something inteligible for the documentation when I have a bit of time.

Copy link
Member

@macdonst macdonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor changes but the PAYLOAD.md doc also has to be updated with the new payload information.

@@ -329,6 +330,12 @@ private void showNotificationIfPossible(Context context, Bundle extras) {
Log.d(LOG_TAG, "title =[" + title + "]");
Log.d(LOG_TAG, "contentAvailable =[" + contentAvailable + "]");
Log.d(LOG_TAG, "forceStart =[" + forceStart + "]");
Log.d(LOG_TAG, "clearNotifications =[" + clearNotifications + "]");

if ("true".equals(clearNotifications)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users may send clearNotifications as a string or as a boolean so we should expect them to mess up and check string equality for "true" and if it is the boolean true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unable to retrieve the value as a boolean even when sent with "clearAllNotifications": true. Java claimed it was a string every time, so this is why I set it to a string.

Any ideas why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without looking into it in more detail but I'd bet it is some kinda of screwy JSON serialization issue on Android.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some of the changes but I'll leave this one for later.

@@ -107,6 +107,15 @@ - (void) application:(UIApplication *)application didReceiveRemoteNotification:(
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"didReceiveNotification with fetchCompletionHandler");

id clearAllNotifications = [userInfo objectForKey:@"clearAllNotifications"];
if ([clearAllNotifications isKindOfClass:[NSString class]] && [clearAllNotifications isEqualToString:@"true"]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as the Android comment. Should check string or boolean values.

if (([clearAllNotifications isKindOfClass:[NSString class]] && [clearAllNotifications isEqualToString:@"true"]) || [clearAllNotifications boolValue])

@macdonst
Copy link
Member

Superseded by #2521

@macdonst macdonst closed this Aug 14, 2018
@lock
Copy link

lock bot commented Sep 13, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants