0

I have this weird situation, my app support iOS7 and above. what it does, it's enabled Remote notifications by using silent notifications.

I know that iOS7 and iOS8 above has different logic to handle notification. I did this :

if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        else {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
        }

here's notification receives

    {
        aps =     {
            "content-available" = 1;
        };

    }

So what it does is, app receive silent notification, and then set localNotification, see below :

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    notification.soundName = UILocalNotificationDefaultSoundName;

    notification.alertBody = @"testing";
    notification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication]  scheduleLocalNotification:notification];

All it works in iOS8 and iOS9 in background mode and foreground mode. When app is in foreground, it will trigger didReceiveLocalNotification.

But when I was testing in iOS7, it doesn't work if the app is in background mode. I'm trying to figure out how this happen, while other OS working fine. I did test while app is open, it did receive push notification, and didReceiveLocalNotification get triggered. but when goes to background, nothing happen (no local push notification).

HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • Possible duplicate of [Silent Push Notification in iOS 7 does not work](http://stackoverflow.com/questions/19239737/silent-push-notification-in-ios-7-does-not-work) – HelmiB Dec 07 '15 at 06:57

3 Answers3

0

You didn't mention about enabling Background Fetch. Have you enabled it in your App Capabilities?

and set this in your AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];

    return YES;
}

and this as your delegate method

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    // do your thing here. maybe calling a view controller class or stuff
}

But if you're not into background fetch, try this instead:

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

Good luck!

adiman
  • 522
  • 9
  • 18
  • I don't enable it, since I don't use `application:performFetchWithCompletionHandler:`. Btw, I reiterate iOS8 & iOS9 works. :( – HelmiB Dec 01 '15 at 12:33
  • any official statement to add sound key ? In the meantime, i will try this. – HelmiB Dec 02 '15 at 07:33
0

Im not exactly sure about the issue, but maybe you can try to change respondsToSelector. Refer to the following:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
Arif Fikri Abas
  • 836
  • 5
  • 9
0

As pointed in this tread, aps need to include priority in order to work in iOS7.

 aps =     {
        badge = 7;
        "content-available" = 1;
        priority = 5;
    }; 

check this : https://stackoverflow.com/a/23917593/554740

Community
  • 1
  • 1
HelmiB
  • 12,303
  • 5
  • 41
  • 68