开发者社区> 问答> 正文

关于获取应用背景模式的本地通知 的问题

应用在背景模式下运行时如何获取本地通知?

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate =[startDate addTimeInterval:60];
NSLog(@"%@",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];

localNotif.alertAction = @"View";

localNotif.soundName = UILocalNotificationDefaultSoundName;

NSString *notifStr=[NSString stringWithFormat:@"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 :)",[itemDict objectForKey:@"fname"]];

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notifStr ,@"notifKey",nil];
localNotif.userInfo = infoDict;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

展开
收起
爵霸 2016-03-19 14:10:23 1858 0
1 条回答
写回答
取消 提交回答
  • -(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)
        {
        }
    }

    看看能不能实现

    2019-07-17 19:08:06
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《 优酷APP全量支持“暗黑模式” 设计与技术完整总结》 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载