2013-04-27 119 views
0

我在3個地方爲您在AppDelegate類新的消息接收遠程推送消息,在didFinishLaunchingWithOptions:(NSDictionary *)launchOptions我在哪裏需要使用代碼的應用程序

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有新的通知。

回答

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。你能分享到底什麼樣的情況?

相關問題