0

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions我使用代码在 3 个地方检查 AppDelegate 类中的新消息:

 if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            //NSLog(@"Launched from push notification: %@", dictionary);
            //notification pending ... so pull from server the new message
             [self addMessageFromRemoteNotification:dictionary updateUI:YES];
        }
    }

并在didReceiveRemoteNotification:(NSDictionary*)userInfo使用代码时:

// notification pending ... so pull from server the new message
[self addMessageFromRemoteNotification:userInfo updateUI:YES];

并在- (void)applicationDidBecomeActive:(UIApplication *)application使用代码

if(application.applicationIconBadgeNumber>0)
    {

         application.applicationIconBadgeNumber = 0;
         // notification pending ... so pull from server the new message
         [self openMessageViewForNewMessage];


    }

但是,我仍然注意到在某些情况下我的应用程序仍然没有“捕获”通知,这意味着它仍然没有意识到它收到了通知。我发信息了吗?或者我应该一直检查“我的服务器”是否有新消息,因为 iOS 可能不会一直通知应用程序有新通知。

4

1 回答 1

1

快速概览...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

您可以在此处指定您希望应用接收哪种通知。例如,如果您想要徽章、声音和警报,您将包括以下内容:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

在里面:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

您可以添加类似这样的内容以确保更新您的徽章:

NSString *badge = [apsInfo objectForKey:@"badge"];
application.applicationIconBadgeNumber = [badge intValue];

我个人还添加了此代码并在适当的情况下进行处理:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedNotificationAlert" object:self];

以上适用于我所有的应用程序。您提到在某些情况下您的应用会丢失 APN。能具体分享一下具体有哪些案例吗?

于 2013-04-27T19:50:03.353 回答