簡體   English   中英

如何在iOS的parse.com中獲取當前安裝的徽章值?

[英]How to get the badge value of current installation in parse.com for ios?

我正在使用parse.com在設備之間發送推送通知。

我正在發送帶有徽章增量值1的推送消息。打開應用程序后,徽章值將設置為零。 以上所有功能均正常運行。 但是,我無法獲得當前安裝的標志值。

按照以下代碼將當前安裝徽標設置為零的文檔

- (void)applicationDidBecomeActive:(UIApplication *)application {
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  if (currentInstallation.badge != 0) {
     currentInstallation.badge = 0;
      [currentInstallation saveEventually];
  }
  // ...   
}

但是,在我的應用中,收到消息后打開應用時, currentInstallation.badge為零。 即,我需要直接將currentInstallation.badge值設置為零,而無需檢查當前的徽章值,如下所示

- (void)applicationDidBecomeActive:(UIApplication *)application {
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  //if (currentInstallation.badge != 0) {
     currentInstallation.badge = 0;
     [currentInstallation saveEventually];
  // }
  // ...
}

一切正常。 但是,使用該徽章值,我需要在我的應用程序內執行其他一些任務。

為什么徽章值對我返回零? 我想念什么?

PFInstallation.badge返回保存到數據庫的徽章的最后一個值。 在您的情況下,它返回零,因為尚未從服務器刷新對象。

有兩種方法可以獲取徽章的價值,然后再將其殺死:

解決方案1 (從UIApplication獲取徽章值)

NSUInteger badgeValue = [UIApplication sharedApplication].applicationIconBadgeNumber;

PFInstallation *installation = [PFInstallation currentInstallation];
installation.badge = 0;
[installation saveEventually];
NSLog(@"%d", (int)badgeValue);

解決方案2 (刷新PFInstallation)

PFInstallation *installation = [PFInstallation currentInstallation];
[installation fetchInBackgroundWithBlock:^(PFInstallation *object, NSError *error) {
    NSUInteger badgeValue = installation.badge;
    installation.badge = 0;
    [installation saveEventually];
    NSLog(@"%d", (int)badgeValue);
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM