locked
Avoid repeated push notifications RRS feed

  • Question

  • User363358 posted

    Hi,

    I have an Xamarin Forms that receives push notifications. The nature of those notifications are sensible to the time they are sent. Imagine Microsoft Authenticator that receives notifications when you are authenticating with an account with 2 factor authentication.

    When I send more than one push notification, on iOS I want to be able to dismiss older notifications and just keep the most recent one.

    Is this handled on DidReceiveRemoteNotification method or is this too late for that? (don't know if this method is called before or after the notification shows on notification centre)

    ``` public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler){ ... // TODO: remove all notifications minus the most recent one. ... }

    ```

    Either way, how can I achieve being able to handle and remove the older push notifications?

    Thanks in advance!

    Thursday, June 28, 2018 10:05 AM

Answers

  • User369979 posted

    A normal remote notification will not trigger the DidReceiveRemoteNotification() event automatically. It will be called when user taps the notification in the notification center to open the app. You can try to use a silent notification to achieve your effect. This kind of notification can be used to inform the application of new content without having the user informed. Instead of displaying a notification alert, the application will be awakened in background and DidReceiveRemoteNotification() will be called. You then have the opportunity to remove the notifications sent before. Here is the way to support silent notification. Firstly, in the info.plist enable the background modes capability => remote notifications. Then the notification payload should be like this:

    {
        "aps" = {
            "content-available" : 1,
            "alert" : "",
            "badge" : 0
        };
        // You can add custom key-value pair here...
    }
    

    The content- available property with a value of 1 lets the remote notification act as a silent notification. Then add a custom key value to sign when app receives this key, it should remove all notifications sent before. Here is an example for you referring to:

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        if (userInfo["shouldRemove"] != null)
        {
            UNUserNotificationCenter.Current.RemoveAllDeliveredNotifications();
        }
        completionHandler(UIBackgroundFetchResult.NewData);
    }
    

    At last you can send a new remote notification and this app will only remain the recent one.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 29, 2018 7:28 AM