1

I have made my own push notification server for my iPhone app. I am sending different push notifications to client devices. Now with one special of the notification, I want to call a specific function in appDelegate or anywhere.

How can I implement this?

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • Whilst I like the idea +1. Are you allowed to do this or will apple reject it. I thought you could only notify the user they had a some sort of notification not start running different functions without them knowing. – Popeye Jan 29 '13 at 17:57
  • @Popeye its just a popup first that will notify them if they want to perform a specific func or not :) I see no reason for objection – Muhammad Umar Jan 29 '13 at 18:03
  • OK. I thought you wanted to do it without letting them know. Misunderstanding that's all. – Popeye Jan 29 '13 at 20:59

2 Answers2

2

you cannot specify a function to call DIRECTLY

when the app is launched, -(BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions is called and passed the note in the options under UIApplicationLaunchOptionsRemoteNotificationKey

when the app is running, you have application:didReceiveRemoteNotification:


you could pass the NAME of a method to call with the notification! so:

...
NSString *methodName = [notificationUserInfo objectForKey:@"methodName"];
[self performSelector:NSSelectorFromString(methodName)];
...

the server side JSON would contain the methodName key: as seen here we can Include all we like APNS JSON PAYLOAD - more arguments

{"aps":{"alert":"APP_NAME': BLA_BLA_BLA","sound":"default"}, "methodName":"xy"}
Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • so on the Server add methodName="xyz" to the userInfo JSON – Daij-Djan Jan 29 '13 at 17:18
  • {"aps":{"alert":"APP_NAME': BLA_BLA_BLA","sound":"default"}} is my payload currently, what u are suggesting? – Muhammad Umar Jan 29 '13 at 17:19
  • @MuhammadUmar see http://stackoverflow.com/questions/5954882/apns-json-payload-more-arguments so in your case {"aps":{"alert":"APP_NAME': BLA_BLA_BLA","sound":"default"}, "methodName":"xy"} – Daij-Djan Jan 29 '13 at 17:33
1

When user launch app via notification, it may have different scenarios:

it wasn't launched, then app launching in default way and you can handle notification in such way:

-(BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UILocalNotification *remoteNotif =
        [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotif) {
        //handle remote notification
    }
    ....
}

if app was in background or foreground, called delegate method

- application:didReceiveRemoteNotification: {
    if (application.applicationState == UIApplicationStateActive){
        //application was in foreground
    } else if (application.applicationState == UIApplicationStateInactive){
        //application was in background
    }
}

Also, if application was in foreground - system does not show alerts, don't change badge icon or playback sound - you should handle notification completely by yourself

Mikhail
  • 4,271
  • 3
  • 27
  • 39
  • The question is how will I get to know what type of notification is being sent. to call a function or not :) – Muhammad Umar Jan 29 '13 at 17:15
  • @MuhammadUmar see my answer. but he is implicity right. you cannot do so DIRECTLY :) but there is a way that I have used in the past too – Daij-Djan Jan 29 '13 at 17:17