我在哪里需要在应用程序中接收远程推送消息

时间:2013-04-27 19:16:21

标签: ios xcode ios6

我使用代码在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可能不会一直通知app有新的通知。

1 个答案:

答案 0 :(得分: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。你能准确分享一下这种情况吗?