帮助处理本地通知!

发布于 2024-10-28 04:48:17 字数 2773 浏览 3 评论 0原文

我尝试了很多博客,但由于某种原因它没有启动!

难道是因为我没有征求许可?我应该向用户询问本地吗?我从苹果的文档中了解到我不知道。

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

    //
//  «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

I tried a verity of blogs but from some reason it doesn't fire!

Can it be because I don't ask permission? Am I suppose to ask the user for local? I understand from apple's documentation that I don't.

This is my singelton class (Checked it is called - with a breakpoint)

    //
//  «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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-11-04 04:48:17

我真的没有看到你的代码有什么问题。确保您的 firedate 不为空。这是我的一个应用程序的工作代码:

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];

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

I don't really see anything wrong with your code. Make sure your firedate is not null. Here is working code form one of my apps:

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];

Also, I'm not sure if it's required but your app may need to be multi-tasking enabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文