1

我为我的 iPhone 应用程序制作了自己的推送通知服务器。我正在向客户端设备发送不同的推送通知。现在有了一个特殊的通知,我想在 appDelegate 或任何地方调用一个特定的函数。

我该如何实施?

4

2 回答 2

2

您不能指定要直接调用的函数

当应用程序启动时,-(BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions被调用并通过选项下的注释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"}
于 2013-01-29T17:16:24.597 回答
1

当用户通过通知启动应用程序时,它可能有不同的场景:

它没有启动,然后应用程序以默认方式启动,您可以通过以下方式处理通知:

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

如果应用程序在后台或前台,则调用委托方法

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

此外,如果应用程序在前台 - 系统不显示警报,不要更改徽章图标或播放声音 - 您应该完全自己处理通知

于 2013-01-29T17:01:43.060 回答