How does Facebook Messenger clear push notifications from the lock screen if you’ve read them on desktop?

后端 未结 4 1090
面向向阳花
面向向阳花 2020-12-07 15:36

When I receive a message on Facebook I get a push notification on a lock screen (iOS). Then I read this message on desktop. Right after that this push notification disappear

相关标签:
4条回答
  • 2020-12-07 16:17

    This is a new feature on iOS 7 called Silent Push Notification, its a multitasking feature.

    What you will need:

    1 - Register for Remote Notifications in didFinishLaunchingWithOptions:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeNewsstandContentAvailability|
          UIRemoteNotificationTypeBadge |
          UIRemoteNotificationTypeSound |
          UIRemoteNotificationTypeAlert)];
    
    }
    

    2 - Implement following method in ApplicationDelegate:

       - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    {
          handler(UIBackgroundFetchResultNewData);
    
    
        // Possibl Results:
    //    typedef enum {
    //        UIBackgroundFetchResultNewData, //Download success with new data
    //        UIBackgroundFetchResultNoData,  //No data to download
    //        UIBackgroundFetchResultFailed   //Downlod Failed
    //    } UIBackgroundFetchResult;
    }
    

    3 - Set UIBackgroundModes inside Application info.plist:

    > <key>UIBackgroundModes</key> <array>
    >     <string>remote-notification</string> </array>
    
    0 讨论(0)
  • 2020-12-07 16:25

    See the Multitasking Enhancements and the silent push notifications in particular. Using the silent push you can notify your app that new content is available and also download that content and/or set the badge number for example without displaying the notification in the "Notification Center". You'll have to set the UIBackgroundModes with the remote-notification value in order to make this work.

    You can send silent push if the user has seen the content on another platform and clear the badge number on your iOS app.

    0 讨论(0)
  • 2020-12-07 16:36

    Warning: I have not tried this, it's just an idea!

    In iOS 7 you could try sending a silent 'content-available' notification to the user. This would wake the app up in the background and allow you to run some code. In the background you could then do

    [[UIApplication sharedApplication] setApplicationBadgeNumber:0];
    [[UIApplication sharedApplication] setApplicationBadgeNumber:newBadgeNumber];
    

    this should clear any notifications that are in the notification centre. You could then post a local notification with the data that came through in your userInfo dictionary, and it would appear as if the old ones were replaced by the new one!

    Again, just an idea...

    0 讨论(0)
  • 2020-12-07 16:39

    One simple way to achieve this effect is to send a normal push notification to your device with a payload that only contains a badge count of 0.

    In Facebook's example, they clearly have enough server power to simply detect when you've read the message on your desktop and send a push to your devices to makes sure that notification is no longer there.

    I'm not saying this is how FB does it but it's a simpler path that may or may not fit your needs. Keep in mind that background tasking consumes your user's battery significantly and should be avoided when possible.

    0 讨论(0)
提交回复
热议问题