3

I m creating an app that has to display CFUserNotificationDisplayAlert even if iPhone Screen is Locked, currently i am using this code

CFOptionFlags responseFlags = 0;
CFUserNotificationDisplayAlert(20.0, 3, NULL, NULL, NULL, CFSTR("Hello"), CFSTR("Hello World"), CFSTR("OK"), NULL, NULL, &responseFlags);

This works great on Home Screen but doesnt pop up if the screen is locked. Is there anything else i have to add to it to make it appear on the Lock Screen as well?

raziiq
  • 549
  • 11
  • 28

2 Answers2

4

You need to use the kCFUserNotificationAlertTopMostKey key.

extern CFStringRef kCFUserNotificationAlertTopMostKey;
CFStringRef keys[] = {
   kCFUserNotificationAlertTopMostKey,
   kCFUserNotificationAlertHeaderKey,
   kCFUserNotificationAlertMessageKey
};
CFStringRef values[] = {
   kCFBooleanTrue,
   CFSTR("Title"),
   CFSTR("Message")
};
CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values,     
                                          sizeof(keys)/sizeof(*keys),
                                          &kCFTypeDictionaryKeyCallBacks,
                                          &kCFTypeDictionaryValueCallBacks);
SInt32 err = 0;
CFUserNotificationRef notif = CFUserNotificationCreate(NULL,
          0, kCFUserNotificationPlainAlertLevel, &err, dict);
CFRelease(dict);
...

See http://iphonedevwiki.net/index.php/CFUserNotification for all dialog description keys for iPhone OS ≤ 3.1.

(Note that while it will show on the lock screen, the phone won't wake up by itself.)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

CFUserNotification is not supported on the iPhone OS. Push Notifications are the iPhone equivalent.

rpetrich
  • 32,196
  • 6
  • 66
  • 89
  • 1
    It is supported, i have this working as i described in my question but the problem is that its not appearing on the lock screen, however it does appear on the screen when iphone is not locked. – raziiq Mar 08 '10 at 05:54
  • 3
    Just because it works, doesn't mean it is supported. We developers on the jailbreak side of the fence use a *ton* of APIs that work (for some definition of work) that aren't supported by Apple. `CFUserNotification`s will *not* show on the lock screen; *push notifications* will. – rpetrich Mar 08 '10 at 06:11
  • Just browsing through your questions and it appears you are a jailbreak dev. If you need to show an alert on the SpringBoard (including the lockscreen), you can create an SBAlertItem subclass at runtime, customize it and activate it--it will show on the lock screen. – rpetrich Mar 08 '10 at 06:16
  • Ya you are right, i can use SBAlertItem subclass but still dont know how to instantiate classes on the runtime, if you have some source on how to do that, please post. – raziiq Mar 08 '10 at 06:47
  • 1
    MobileSubstrate has an example of creating a new class at runtime: http://svn.saurik.com/repos/menes/trunk/mobilesubstrate/MobileSafety.mm It even uses SBAlertItem :) – rpetrich Mar 08 '10 at 06:51
  • ahhh, again mobilesubstrate, tried getting hold of it quite sometimes but its really complex, tried ipodtouchfansforum tutorials also but to no avail. You know of any good tutorial on mobilesubstrate?? – raziiq Mar 08 '10 at 06:59
  • 3
    `objc_allocateClassPair` to create a new class, then add methods using `class_addMethod`, then `objc_registerClassPair`, and finally you can send `alloc` to the class to create an new instance. After that, it's `SBAlertItem`-specific code. – rpetrich Mar 09 '10 at 04:27
  • 3
    Also, best place to get help on jailbreak-specific development is in #iphone on irc.saurik.com – rpetrich Mar 09 '10 at 04:29