簡體   English   中英

推送通知到達時,應用程序徽章圖標未更新

[英]application badge icon not updated when push notification arrives

我已經查看了有關在午夜更新應用程序徽章的問題, 其中包含以下選項:應用程序未啟動或在后台運行,徽章編號可能會減少,並且在通知到達時更改應用程序圖標徽章,並且當應用程序關閉 推送通知徽章計數不更新更新徽章圖標更多,但我有同樣的問題,當應用程序在后台和推送通知到達時,應用程序徽章圖標不更新。

我檢查了所有可能性。 我的代碼是:

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

    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)]) {
        appState = application.applicationState;
    }

    for (id key in userInfo) {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    NSLog(@"remote notification: %@",[userInfo description]);

    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo valueForKey:@"aps"] valueForKey:@"badge"] integerValue];

    UIApplicationState state = [application applicationState];


    if (state == UIApplicationStateActive)
    {
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];

    }
 }

是的,當app在前台時工作正常,然后它顯示警告正常。 但當應用程序處於后台時,既不會發生任何事件,也不會更新應用程序徽章圖 我還設置了背景模式,如ios7.1所示:推送通知徽章更新問題 ,我還測試了應用程序徽章圖標是否正確並在應用程序處於前台時打印在NSLog中。 當應用程序在后台運行並且工作正常時,我需要將未讀消息設置為徽章圖標,但是當任何新推送通知到達時應該更新它。 請建議我選擇。

最后這個問題解決了。 確實,當app處於后台時,沒有一個動作被觸發。 所以當app在后台時, appDelegate didReceiveRemoteNotification方法也不會被觸發。

當任何新推送通知到達設備時,應用程序徽章圖標由iOS設置。 有效載荷與@rahulPatel說的相同。 我做了一點改變,因為BADGE_COUNT是從數據庫中提取的,雖然它是INTEGER但是考慮到字符串和字符串不被iOS采用,這就是為什么我的應用程序的徽章圖標在通知到達時不會更新。 現在我從服務器端的代碼變成這樣:

$badges=(int)$cntmsg;

$body['aps'] = array(
                'alert' => $message,
                'sound' => 'default',
                'badge' => $badges
                //'msg_id'=> $msg_id
            );

所以通過將計數msg更改為(int)它解決了我的問題。 謝謝。

通常在所有應用中,未讀通知計數都在服務器中維護。 當服務器向特定設備發送推送通知時,令牌服務器會發送徽章計數以及有效負載

您的服務器邏輯需要跟蹤正確的徽章計數並適當地發送它。

{
    "aps" :  
    {
        "alert" : "Your notification message to show",
        "badge" : BADGE_COUNT ,
        "sound" : "sound.caf"
    }
}

暫無
暫無

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

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