How does Facebook Messenger clear push notificatio

2019-01-16 08:35发布

问题:

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 without any interactions with the phone. How can I implement the same thing myself for removing outdated notifications?

The second usage could be stitching notifications together. For instance Instagram sends you a push when someone liked your photo. After 20 likes your notifications screen is ruined and unreadable. But using the same principal as Facebook does it seem to be possible remove previous notification of the same sort and create new with the increased counter. No "User A liked photo X, User B liked photo Y etc", but "20 users liked photo Z" instead.

I've seen couple of treads on similar topics here, but still no answer so far. Thanks!

回答1:

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.



回答2:

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.



回答3:

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>


回答4:

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...