【问题标题】:How to notify a notification message at every month end day如何在每个月末日通知通知消息
【发布时间】:2016-08-13 20:16:46
【问题描述】:

如何通知每月通知。

-(void)applicationDidEnterBackground:(UIApplication *)application {

        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        format.dateFormat = @"dd-MM-yyyy";
        NSComparisonResult result = [[format stringFromDate:[NSDate new]] compare:[format stringFromDate:[self lastDayOfMonthOfDate:[NSDate date]]]];
        if (result == NSOrderedSame){
            if (![USERDEFAULTS boolForKey:@"IS_MONTH"]) {
                [self getNotifiedForEveryMonth:nil];
            }}
        else{
            [USERDEFAULTS setBool:NO forKey:@"IS_MONTH"];
        }
    }

// getNotifiedForEveryMonth

-(void)getNotifiedForEveryMonth:(id)userinfo{
        // Schedule the notification
    [USERDEFAULTS setBool:YES forKey:@"IS_MONTH"];
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate date];
    localNotification.alertBody = @"Every Month Notificiation";
    localNotification.alertAction = @"Show me the item";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
 }

-(NSDate*)lastDayOfMonthOfDate:(NSDate *)date
{
        //    NSGregorianCalendar
    NSInteger dayCount = [self numberOfDaysInMonthCountForDate:date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *comp = [calendar components:
                              NSCalendarUnitYear |
                              NSCalendarUnitMonth |
                              NSCalendarUnitDay fromDate:date];
    [comp setDay:dayCount];
    return [calendar dateFromComponents:comp];
}

-(NSInteger)numberOfDaysInMonthCountForDate:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone timeZoneWithName:TIME_ABSOLUTE]];
    NSRange dayRange = [calendar rangeOfUnit:NSCalendarUnitDay
                                      inUnit:NSCalendarUnitMonth
                                     forDate:date];
    return dayRange.length;
}

上面的代码与我尝试通知通知。 每个月末都会收到通知,

如果用户关闭了通知,然后下个月来了,通知没有通知。

安装应用程序后,如何在月底通知用户通知消息。

感谢您的意见。

【问题讨论】:

  • 所以基本上你想在每个月的最后一天发出通知?
  • @Harsh 是的,每个月结束。
  • 为什么你的应用程序进入后台时会抛出通知?
  • 它回到应用程序,说你有一些与应用程序有关的事情。如果用户删除通知,如果应用程序在后台,仍然希望他抛出通知怎么办。每个月的基地。
  • 你能解释清楚你想要达到的目标吗?

标签: ios objective-c appdelegate localnotification


【解决方案1】:

1) 如果您希望每个月触发通知,即使用户超过一个月没有打开应用程序,您也应该设置多个通知(最多 60 个)。例如,设置未来 12 个月的通知。

2) 您应该通过此代码设置通知(循环,12 次):

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

3) 在设置新通知之前,请使用以下代码删除旧通知:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2017-02-23
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多