帮助处理本地通知!

时间:2011-03-29 01:00:54

标签: iphone notifications uilocalnotification

我尝试了一些博客,但出于某种原因,它并没有开火!

是不是因为我没有得到许可?我想问用户是否在当地?我从苹果的文档中了解到我没有。

这是我的singelton类(检查它被称为 - 带断点)

    //
//  «FILENAME»
//  «PROJECTNAME»
//
//  Created by «FULLUSERNAME» on «DATE».
//  Copyright «YEAR» «ORGANIZATIONNAME». All rights reserved.
//  File created using Singleton XCode Template by Mugunth Kumar (http://mugunthkumar.com
//  Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above

#import "NotifierSingelton.h"

static NotifierSingelton* _instance;

@implementation NotifierSingelton

+ (NotifierSingelton*)sharedInstance
{
    @synchronized(self) {

        if (_instance == nil) {

            // iOS 4 compatibility check
            Class notificationClass = NSClassFromString(@"UILocalNotification");

            if(notificationClass == nil)
            {
                _instance = nil;
            }
            else
            {
                _instance = [[super allocWithZone:NULL] init];

            }


            // Allocate/initialize any member variables of the singleton class her
            // example
            //_instance.member = @"";
        }
    }
    return _instance;
}

- (void) scheduleNotificationOn:(NSDate*) fireDate
                           text:(NSString*) alertText
                         action:(NSString*) alertAction
                          sound:(NSString*) soundfileName
                    launchImage:(NSString*) launchImage
                        andInfo:(NSDictionary*) userInfo

{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = fireDate;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];  

    localNotification.alertBody = alertText;
    localNotification.alertAction = alertAction;    

    if(soundfileName == nil)
    {
        localNotification.soundName = UILocalNotificationDefaultSoundName;
    }
    else
    {
        localNotification.soundName = soundfileName;
    }

    localNotification.alertLaunchImage = launchImage;

    //self.badgeCount ++;
    localNotification.applicationIconBadgeNumber = 1;
    localNotification.userInfo = userInfo;

    // Schedule it with the app
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];
}


#pragma mark Singleton Methods

+ (id)allocWithZone:(NSZone *)zone
{   
    return [[self sharedInstance]retain];   
}


- (id)copyWithZone:(NSZone *)zone
{
    return self;    
}

- (id)retain
{   
    return self;    
}

- (unsigned)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;    
}

@end

1 个答案:

答案 0 :(得分:0)

我真的没有看到你的代码有什么问题。确保您的被激活的非空。以下是我的应用程序之一的代码:

notif = [[UILocalNotification alloc] init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *raceDate = [df dateFromString:[raceDates objectAtIndex:i]]; 
notif.fireDate = raceDate;
notif.timeZone = [NSTimeZone timeZoneWithName:@"WAT"];

notif.alertBody = NSLocalizedString(@"NotificationBody", @"");
notif.alertAction = NSLocalizedString(@"NotificationButton", @"");
notif.soundName = @"push.aif";

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

此外,我不确定是否需要,但您的应用可能需要启用多任务处理。