左上的遊戲iCon顯示了數字1,用來提醒用戶去查看消息之類的功能,這個需要我們自己去操作顯示數字。
完整的操作方法是,利用UILocalNotification:
1 2 3 4 |
UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.applicationIconBadgeNumber = pushCount; UIApplication *app = [UIApplication sharedApplication]; [app scheduleLocalNotification:notification]; |
今天在嘗試修改applicationIconBadgeNumber的時候我做了一個操作,卻沒有效果:
1 |
notification.applicationIconBadgeNumber += 1; |
後來查資料發現,這樣的操作並沒有用,
但是可以直接賦值:
1 |
notification.applicationIconBadgeNumber = 2; |
所以如果我們想要做到,顯示出用戶未讀消息數量的時候,可以實現進行計算在賦值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
FMDatabase *db = [[FMDatabase alloc]initWithPath:DBPath]; if(![db open]){ NSLog(@"open db error when initDB"); return; } NSString *sql = @"select count(push_type) as 'count' from PushInfo where is_checked ='0'"; FMResultSet *set = [db executeQuery:sql]; NSInteger pushCount = 0; while([set next]){ pushCount = [[set objectForColumnName:@"count"]integerValue]; } [db close]; UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.applicationIconBadgeNumber = pushCount; UIApplication *app = [UIApplication sharedApplication]; [app scheduleLocalNotification:notification]; |
但是我發現,如果我前面刪除未讀消息時,每次都調用到這個方法,有時候會失靈,
所以我們可以設定,當用戶開著App的時候不調用方法,而是在要推出App時才調用:
1 2 3 |
- (void)applicationWillResignActive:(UIApplication *)application{ [UIApplication sharedApplication].applicationIconBadgeNumber = [TSPushClient unReadPushNotificationNumber]; } |