didReceiveRemoteNotification:fetchCompletionHandler not being called when app is in background and not connected to Xcode

前端 未结 10 1605
别那么骄傲
别那么骄傲 2020-11-28 04:27

I\'ve a very strange problem, I implemented:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchComp         


        
相关标签:
10条回答
  • 2020-11-28 05:05

    There could be number of things might have gone wrong, The first from my own experience. In order to make silent push notification work. Your payload has to be structured correctly,

    {
        "aps" : {
            "content-available" : 1
        },
        "data-id" : 345
    }
    

    Does your push message has content-available: 1 if not then iOS will not call the new delegate method.

    - (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    
    0 讨论(0)
  • 2020-11-28 05:06

    Hey guys its very simple you can call your method

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
    }
    

    Steps:

    1. project -->Capablities--> Background Modes and select check boxes of "Background Fetch" & "Remote notifications", or go into .plist and select a row & give name "Background Modes" and it will create with an array, set "Item 0" with string "Remote notifications".

    2. say to server side developer that he should send

      "aps" : { "content-available" : 1 }

    thats it now you can call your methods:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
    }
    
    0 讨论(0)
  • 2020-11-28 05:06

    Spent two days on this! Before checking your code and your push params - check that you are not on LOW POWER MODE!!!(and Background App Refresh is ON) as you connect your device to xCode==power it will work, but if you will disconnect it - low power mode will disable background app refresh.

    0 讨论(0)
  • 2020-11-28 05:11

    Issue have been fixed in iOS 7.1 Beta 3. I double checked and I confirm it's working just fine.

    0 讨论(0)
  • 2020-11-28 05:15

    This was an issue for me today and I was baffled.

    iOS: 10.2.1 xCode: 8.2.1 Swift: 3.0.2

    The issues was only on one phone I would get the packed only when plugged into xCode.

    I re-read Apples push documentation in case I missed something with the new UserNotifications framework and or messed something up with my code to fall back to the depreciated delegate functions for iOS 9.

    Anyway, I noticed this line in the documentation for application:didReceiveRemoteNotification:fetchCompletionHandler::

    "Apps that use significant amounts of power when processing remote notifications may not always be woken up early to process future notifications."

    It's the very last line on the page.

    While I wish Apple told me more, it turns out a simple phone restart solved the problem for me. I really wish I could figure out exactly what went wrong, but here are my very speculative conclusions:

    1) Push notifications were not being delivered to this app on this particular phone because of the line in the documentation mentioned above.

    2) When plugged into xCode iOS is ignoring the above, documented rule.

    3) I checked the (notoriously confusing) battery percentage calculator in system settings. It showed my app at a modest 6%, BUT Safari was a whopping 75% on this phone for some reason.

    4) After phone restart, Safari was back down to about 25%

    5) Push worked fine after that.

    So... My ultimate conclusion. To weed out the documented battery issue either try a phone restart or try a different phone and see if the problem persists.

    0 讨论(0)
  • 2020-11-28 05:16
    TL;DR: Use Test Flight in iTunes Connect
    

    Maybe some of you guys already figured out this, but I posting here since I don't see a clear answer.

    The had the exact same problem describe. Silent push notifications worked while the Lightning cable was connected. Stopped working when I disconnected the cable. I had every NSLog and network call tracked to prove that was indeed happening.

    I was using the payload suggested in many answers, as well as this one:

    {
        "aps" : {
            "content-available": 1,
            sound: ""
        }
    }
    

    After many hours, I discovered that the issue is related to Adhoc and Development provisioning profiles, on iOS 8.0, 8.1 and 8.1.1. I was using Crashlytics to send beta versions of my app (that uses Adhoc profile).

    The fix is:

    In order to have it working, try out Apple's Test Flight integration with iTunes Connect. Using that you will send an Archive of your app (the same archive to be used on App Store) but enable your binary to be used in beta. The version installed from Test Flight probably (I can't prove) uses the same Production Provisioning Profile from the App Store, and the app works like a charm.

    Here's a link that helps set up the Test Flight account:

    http://code.tutsplus.com/tutorials/ios-8-beta-testing-with-testflight--cms-22224

    Not even a single Silent Payload was missed.

    I hope that helps someone not to lose a whole week on this issue.

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