2011-03-29 33 views
0

我嘗試了一些博客,但是由於某種原因它不會啓動!本地通知幫助!

難道是因爲我沒有要求許可嗎?我想問問用戶本地?我從蘋果的文檔中瞭解到我沒有。

這是我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 

回答

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

而且,我不知道這是否是必需的,但你的應用程序可能需要多任務啓用。

+0

謝謝,我會嘗試一下代碼。我的「應用程序不在後臺運行」已設置。這是唯一的多任務屬性嗎?因爲它仍然不起作用。 – AYBABTU 2011-03-29 01:16:44

+0

我不知道爲什麼,但你的代碼做到了!這花了我太久了!幾個小時的絕望! – AYBABTU 2011-03-29 01:27:32

+0

我在開始時也遇到了很多問題。這比本來要困難得多。很高興我能幫上忙! – 2011-03-29 18:11:21