iphone - 后台本地通知

标签 iphone ios objective-c notifications uilocalnotification

谁能告诉我如何在我的应用程序处于后台时获取 UILocalNotification

我在这里发布我的代码。提前致谢。

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;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

最佳答案

-(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)
    {
    }
}

希望这对你有帮助:)

关于iphone - 后台本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16811466/

相关文章:

iphone - 无法从SQLite数据库删除行,但未发出错误

iphone - 呈现一个没有 nib 文件的 View Controller

ios - 无法使用类型为 '*' 的参数列表调用 '($9 floatLiteralConvertible)'

iphone - PhoneGap 应用程序会在应用程序更新时自行重置吗?

iphone - UIKeyboardType NumberPad - 在 iPad 上仅显示数字?

ios - 如何将单元格编号发送到 TableViewCell 中的函数

ios - 在 UIImage 上绘制删除透视,露出下面的 UIImage 图层

ios - 类似 Twitter 的滚动标题

iphone - CoreLocation GPS 状态栏指示器不消失

objective-c - 使用 NSTask 运行 shell 脚本会导致 posix_spawn 错误