Local Notification in background

旧街凉风 提交于 2019-11-28 07:02:01
-(void)insert:(NSDate *)fire
{
    [[UIApplication sharedApplication]cancelAllLocalNotifications];
    self.localNotification = [[UILocalNotification alloc] init];
    if (self.localNotification == nil)
    {
        return;
    }
    else
    {
        self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
        self.localNotification.alertAction = nil;
        self.localNotification.soundName = UILocalNotificationDefaultSoundName;
        self.localNotification.alertBody = @"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)";
        self.localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
        self.localNotification.applicationIconBadgeNumber=1;
        self.localNotification.repeatInterval=0;
        [[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
    }
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    [[UIApplication sharedApplication]cancelAllLocalNotifications];
    app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber -1;

    notif.soundName = UILocalNotificationDefaultSoundName;

    [self _showAlert:[NSString stringWithFormat:@"%@",Your msg withTitle:@"Title"];

}

- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title
{
    [self.alertView_local removeFromSuperview];
    self.alertView_local = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [self.alertView_local show];

    if (self.alertView_local)
    {
    }
}

Hope this will help you :)

addTimerInterval is depcrecated in iOS 4.0, make sure you deployment target. You can use.

- (id)dateByAddingTimeInterval:(NSTimeInterval)ti

Also make sure you set the correct value for fireDate

Please Use below code.

 self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    self.localNotification.alertAction = nil;
    self.localNotification.soundName = UILocalNotificationDefaultSoundName;
    self.localNotification.alertBody = @"Hey looks like you're meeting up with %@, why don't you let your other friends know what fun they're missing out on? Share a photo :)";
    self.localNotification.alertAction = NSLocalizedString(@"Read Msg", nil);
    self.localNotification.applicationIconBadgeNumber=1;
    self.localNotification.repeatInterval=0;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!