1

I have configured a local push notification category with two actions. The push notifications run fine, and I am handling these notifications in the application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier method inside my AppDelegate.

My question now is how can I go about only displaying such notifications on the user's lock screen? My assumptions about the willResignInactive method is that the app can still run after the user locks the device, for a while at least. I would like a way to just fire the notification exactly after the user locks the device so the custom actions are usable from the lock screen.

Any help would be appreciated.

Alex Wulff
  • 2,039
  • 3
  • 18
  • 29
  • What do you mean only lock screen? If the person gets a notification, he gets a notification no matter what's he doing on the phone. And if the phone is unlocked it will display at the top, if you decide not to click it, it will move straight to the notification centre. – emotality Nov 01 '15 at 14:16
  • @apptality I would like to schedule the local notification when the user locks the device, so they can interact with the custom actions on the lock screen via the notification. – Alex Wulff Nov 01 '15 at 14:18

2 Answers2

2

I don't think you have access to that specific event as it is out of your app jurisdiction thats why there is applicationDidBecomeActive, applicationWillTerminate etc. Those are your borders, but you can time it after 10 mins and hope they locked their phone? Or to be more accurate you can check if they did put their phone down, check this answer:

Lock Unlock events iphone

Community
  • 1
  • 1
emotality
  • 12,795
  • 4
  • 39
  • 60
0

Using some details from @apptality I was able to get this working with Darwin Notifications. This answer explains how to get the notifications working. I used the following code to do this:

in AppDelegate's didFinishLaunchingWithOptions method place the following code :

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                               (void*)self, // observer (can be NULL)
                               lockStateChanged, // callback
                               CFSTR("com.apple.springboard.lockcomplete"), // event name
                               NULL, // object
                               CFNotificationSuspensionBehaviorDeliverImmediately);

Then use this method to handle the notifications:

static void lockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSLog(@"event received!");
if (observer != NULL) {
    AppDelegate *this = (__bridge AppDelegate*)observer;

    NSString* notifyName = (__bridge NSString*)name;

    if ([notifyName isEqualToString:@"com.apple.springboard.lockcomplete"]) {
        [this localNotificationSetup];
    }
}
}

where localNotificationSetup is where you register your notifications for delivery. Be careful as this is a c-based method so much of your objective-c code needs changing. You can then use this method to handle actions on the notification:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler 

To actually accomplish anything useful you may need to load data from nsuserdefaults in this method if your app goes to sleep.

Community
  • 1
  • 1
Alex Wulff
  • 2,039
  • 3
  • 18
  • 29