2014-03-27 77 views
4

我在當前的一個項目中設置了Push Notification。我已按照推送通知所需的所有說明進行操作。在[tag:ios7]中工作正常,但在7.1我在後臺模式下的應用程序中發現了徽章更新問題。ios7.1:推送通知徽章更新問題

我的代碼是以下內容: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 

} 


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error 
{ 
    NSLog(@"Failed to get token, error: %@", error); 
} 


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    NSString *str = [NSString 
        stringWithFormat:@"%@",deviceToken]; 
    NSLog(@"%@",str); 

    self.deviceToken = [NSString stringWithFormat:@"%@",str]; 
    NSLog(@"dev --- %@",self.deviceToken); 
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@">" withString:@""]; 
    NSLog(@"dev --- %@",self.deviceToken); 
} 

併爲獲得性反應

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

    UIApplicationState state = [application applicationState]; 

    // If your app is running 
    if (state == UIApplicationStateActive) 
    { 

     //You need to customize your alert by yourself for this situation. For ex, 
     NSString *cancelTitle = @"ok"; 
     // NSString *showTitle = @"Get Photos"; 
     NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:nil]; 
     [alertView show]; 


    } 
    // If your app was in in active state 
    else if (state == UIApplicationStateInactive) 
    { 

    } 

    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue]; 

} 

的第一件事是,當我的應用程序要回地面是didReceiveRemoteNotification沒有得到所謂的,所以我做了一些搜索,並把: -

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 

並設置背景模式,如: -

enter image description here

,所有工作正常,當我的設備是在Xcode和連接i上運行應用程序測試推送通知但當我拔下設備(iOS7.1)。然後推送通知到達但徽章不更新。否則,在後臺更新徽章以及調用所有方法。但同樣的事情,我測試這個應用程序到我的另一臺設備iOS7工作得很好。

我不明白我的錯誤在哪裏,而且我在代碼中做錯了。有沒有任何錯誤或我不認爲,所以請幫我解決這個問題。

+0

你的服務器是否在'apn'發送了一個徽章? – cojoj

+0

我正在設置我身邊的徽章,正如我在上述問題中提到的。 –

+1

但是您正在使用遠程通知...它是管理徽章的服務器端,而不是設備上的您。 – cojoj

回答

1

嘗試:

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber; 
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1 
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue]; 
0

最近我有這個問題,我不得不做一些轉換:

NSDictionary *apsData = [userInfo objectForKey:@"aps"]; 
NSInteger badgeNumber = (NSInteger)[[apsData objectForKey: @"badge"] intValue]; 

[UIApplication sharedApplication].applicationIconBadgeNumber = 
      [UIApplication sharedApplication].applicationIconBadgeNumber + badgeNumber; 

我用PHP將數據發送到蘋果服務器,所以我也做了一個投在那裏:

$playload = array('aps'=>array(
         'message'=> 'some message', 
         'badge'=> (int)$badge 
       ) 
); 
+0

請注意語法可能有點關閉,但你得到的點 – meda

+0

我這樣做,但只發現在iphone4和4s,iphone5和5s工作正常的問題。作爲'NSInteger'鑄造的 –

+0

?它是iPhone模型還是iOS版本,它們是有所不同的 – meda