9

我看到我的推送通知有一种特殊的行为,想知道是否有人对我做错了什么或应该做什么有任何建议。

我的application:(UIApplication*)application didReceiveRemoteNotification:写法如下:

- (void)application:(UIApplication*)application didReceiveRemoteNotification: (NSDictionary*)userInfo
{
NSLog(@"Received notification: %@", userInfo);
[self addMessageFromRemoteNotification:userInfo updateUI:YES];
} 

如您所见,我并不担心应用程序的状态。我只想在收到 PN 时记录一条消息。

我的设置是根据 Apple 的文档进行的,我可以接收推送通知。

以下是 PN 进入时的预期行为:

  • 应用程序在后台,我点击通知:我可以在通知中心看到通知。点击通知,应用程序来到前台,我可以看到上面的方法被调用了。
  • 应用程序处于活动状态并且已经在前台:我可以看到上面的方法被调用。

现在,以下是我看到的特殊行为:

  • PN 进来了。在通知中看到它,看到应用程序图标上的徽章。我单击应用程序图标 - 而不是通知:在这种情况下,我发现没有调用上述方法。我期待它被称为...

有没有人见过这种行为?这是应该发生的事情吗?我在 Apple 文档中看不到任何关于此的内容......另外,有没有办法解决这个问题?

4

3 回答 3

2

我有同样的行为。它让我发疯,但我认为这就是 iOS 的工作方式。

下面是苹果文档的摘录。这是关于应用程序未运行时的应用程序:didFinishLaunchingWithOptions。当应用程序处于后台/didReceiveRemoteNotification 时,它看起来是一样的。

“如果点击操作按钮(在运行 iOS 的设备上),系统启动应用程序并且应用程序调用其委托的 application:didFinishLaunchingWithOptions: 方法(如果已实现);它传入通知负载(用于远程通知)或本地-通知对象(用于本地通知)。

如果在运行 iOS 的设备上点击应用程序图标,应用程序会调用相同的方法,但不会提供有关通知的信息。" 来自:http: //developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

于 2012-12-07T11:54:50.203 回答
2

如果应用程序没有在后台运行,但最初是从推送通知启动的,并且您已经实现了didFinishLaunchingWithOptions:,您需要在那里实现您的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    if (launchOptions != nil) {
        NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
        if (userInfo != nil) {
            NSDictionary* apsInfo = [userInfo objectForKey:@"aps"];
            NSString* custom = [userInfo objectForKey:@"yourCustomPushData"];
            // do something with it
        }
    }
    //...
}
于 2012-06-06T09:35:25.020 回答
0

Mrj's 在我的情况下不起作用,所以我尝试了以下选项

如果应用程序不在后台,您应该使用以下代码

   //-------------- check notification when app is come to foreground after apllication get terminated ----------------//

UILocalNotification *localNotif =

[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (localNotif) {

    [self handleRemotNotification:[launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]]; // private method


}
于 2013-06-06T07:07:00.190 回答