[英]Push notification applicationIconBadgeNumber is not update in ios7
我在應用程序委托中使用以下代碼來接收遠程通知,但是當應用程序在后台運行時,其applicationIconBadgeNumber(以紅色/白色顯示在應用程序圖標的左上角)不會更新。 當推送通知收到時,它以幻燈片動畫的形式出現在屏幕的右上角,通知有效負載中的徽章計數正好來自服務器端。
在didFinishLaunchingWithOptions
我輸入了以下代碼
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
遠程通知代表:
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *deviceTokenTrimmed = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
currentdeviceToken=deviceTokenTrimmed;
[[NSUserDefaults standardUserDefaults] setValue:deviceTokenTrimmed forKey:@"pushtoken"];
NSLog(@"Device Token didRegisterForRemoteNotificationsWithDeviceToken : %@",deviceTokenTrimmed);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Device Token in FailToRegister RemoteNotifications ERROR %@",error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"%@",userInfo);
if (application.applicationState == UIApplicationStateActive)
{
/***********code to show alert********/
if (![[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]] isEqualToString:@""] && [[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]]!=nil) {
NSString *MSG =[[NSString alloc]initWithString:[[userInfo objectForKey:@"aps"] objectForKey: @"alert"]];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:MSG delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Notification Received." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[alert show];
}
}else{
application.applicationIconBadgeNumber =[[[userInfo objectForKey:@"aps"] objectForKey: @"badge"]integerValue];
}
}
提前致謝。
我發現了sol,實際上是application:didReceiveRemoteNotification在后台應用程序時未調用,在后台運行app時,它有責任更新applicationIconBadgeNumber,在我的情況下,實際問題出在有效載荷中。 從服務器端將其設置為字符串,但應為整數。 我正在發布用php編寫的有效載荷正確代碼。
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type
);
謝謝,mokujin和Himanshu
UIApplicationDelegate
使用方法
由於application:didReceiveRemoteNotification:
方法僅在應用程序處於活動狀態時運行,因此無論應用程序的狀態如何,系統都會調用此方法。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSInteger badgeNumber = [application applicationIconBadgeNumber];
[application setApplicationIconBadgeNumber:++badgeNumber];
}
如果您實際上是在設置JSON數字,請設置徽章編號。 唯一的解決方案是通過在每次讀取“通知”時通知服務器來管理服務器端。
我給出了一個邏輯,即如何在Google中實現搜索,與此相關的文章很多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.