Ios 应用程序徽章编号不会随本地通知而增加

Ios 应用程序徽章编号不会随本地通知而增加,ios,uilocalnotification,badge,Ios,Uilocalnotification,Badge,我一直在处理本地通知。但问题是,应用程序图标徽章编号不会随着本地通知的触发而增加。我试过了。applicationBadgeNumber+1; 但它没有任何效果。应用程序图标徽章编号始终为1 enter code here - (IBAction)save:(id)sender { eventitem=textfield.text; NSDate *newdate=self.datepick.date; UIApplication *app=[UIApplicat

我一直在处理本地通知。但问题是,应用程序图标徽章编号不会随着本地通知的触发而增加。我试过了。applicationBadgeNumber+1; 但它没有任何效果。应用程序图标徽章编号始终为1

enter code here
    - (IBAction)save:(id)sender
{
    eventitem=textfield.text;
    NSDate *newdate=self.datepick.date;
    UIApplication *app=[UIApplication sharedApplication];
    notifyalarm=[[UILocalNotification alloc]init];
    if (notifyalarm)
    {
        notifyalarm.fireDate=newdate;
        notifyalarm.timeZone=[NSTimeZone defaultTimeZone];
        notifyalarm.alertBody=eventitem;
        notifyalarm.applicationIconBadgeNumber=[UIApplication sharedApplication] .applicationIconBadgeNumber+1;

        [app scheduleLocalNotification:notifyalarm];

    }
}更新

看到您的代码后,我建议在设置徽章值之前使用以下命令

NSUserDefaults* userDefs = [NSUserDefaults standardUserDefaults];
//old val
NSInteger iconBadge = [userDefs integerForKey:@"myBadgeVal"];
//updatge val
iconBadge++;
//store
[userDefs setInteger:iconBadge forKey:@"myBadgeVal"];
[userDefs synchronize];
//set as icon badge
notifyalarm.applicationIconBadgeNumber=iconBadge;
但是,我不确定何时调用“save”方法。确保调用此方法的次数与预期次数相同


您必须在本地处理此数字,如[UIApplication sharedApplication].ApplicationOnBadgeNumber将始终为0(因为您不会在任何地方更新此值)。如果愿意,可以使用NSUserDefaults。另外,请提供一些代码,以便我们能提供更多帮助。

如果您的第一个解决的
本地通知将首先触发,那么您可以使用以下方法设置
徽章计数

notifyalarm.applicationIconBadgeNumber = ([[[UIApplication sharedApplication] scheduledLocalNotifications] count] + 1);

从后台启动应用程序时,徽章编号不会更新。也就是说,如果徽章号码是3,然后我启动应用程序到前台,设置另一个通知,并使我的应用程序到后台。因此,当新通知被触发时,徽章编号变为4,而不是我之前告诉过的1。如果您的第一个已解决的通知先被触发,则只有上述逻辑将起作用。