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

与世无争的帅哥 提交于 2019-11-28 03:34:28
Ivan Genchev

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.

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.

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!