2011-11-04 11 views
9
UILocalNotification *notif = [[cls alloc] init]; 
notif.fireDate = [self.datePicker date]; 
notif.timeZone = [NSTimeZone defaultTimeZone]; 

notif.alertBody = @"Did you forget something?"; 
notif.alertAction = @"Show me"; 

ユーザーが「showme」をクリックしてアプリを開くとアラートが表示されるはずです。 どこにこのコードを書くべきですか?可能であれば、少しコードを教えてくださいUILocalNotificationのアラートアクションのコード

答えて

24

通知が発生した時点でのアプリの状態に応じて、UILocalNotificationの通知が2か所で届きます。

1.In アプリケーション:didFinishLaunchingWithOptions:メソッド(アプリが実行中でもバックグラウンドでもない場合)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    ... 
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (localNotif) {  
     // Show Alert Here 
    } 
    ... 
} 

2.In アプリケーション:didReceiveLocalNotification:方法アプリケーションが実行中またはバックグラウンドで実行されているいずれかの場合。アプリが既に動作しているときに警告を表示することはほとんど役に立たない。だから、通知が発せられた時点でアプリがバックグラウンドになっていた場合にのみ警告を表示する必要があります。アプリがバックグラウンドから再開しているかどうかを確認するには、applicationWillEnterForeground:メソッドを使用します。

- (void)applicationWillEnterForeground:(UIApplication *)application { 

    isAppResumingFromBackground = YES; 
} 

あなたはdidReceiveLocalNotificationにアラートを表示することができ、これを使用する:アプリがバックグラウンドから再開されるだけ方法。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 

    if (isAppResumingFromBackground) { 

     // Show Alert Here 
    } 
} 

アラートビューに通知が関係なく、アプリケーションの状態の発射されるすべての時間を表示したい場合は、単純にあれば、条件を省略することができます。

+0

アラートが正常に機能しています。そのアラートに表示されるのは他のView Controllerからのもので、なぜ動作していないのですか? 'application.applicationIconBadgeNumber = 0; \t NSString * reminderText = [notification.userInfo objectForKey:kRemindMeNotificationDataKey]; \t [viewController showReminder:reminderText]; – Chandu

+0

show reminderは、他のView Controllerで書いたメソッドですが、なぜ呼び出されないのか分かりません。 – Chandu

+0

ローカルnotifを処理するには良い方法です。 'isAppResumingFromBackground'を' YES'に設定する場所をお勧めしますか?いくつかの場所は 'application:didReceiveLocalNotification:'の後に呼び出されますが、アプリが再開するどのような場合でも呼び出されます。 – dvkch

0

didreceivelocalnotificationと呼ばれるデリゲートメソッドがあります。これをアプリケーションdelegate.Youに書く必要があります。ユーザーがこの代理人をクリックするとメソッドが呼び出されます.Defreceivelocalnotifactionメソッドで任意のコードを記述します。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
} 
+0

- (ボイド)アプリケーション:(のUIApplication *)アプリケーション didReceiveLocalNotification:(UILocalNotification *)通知{ \t \t application.applicationIconBadgeNumber = 0。 \t NSStringの* reminderText = [notification.userInfo \t \t \t \t \t \t \t objectForKey:kRemindMeNotificationDataKey]。 \t [viewController showReminder:reminderText]; } – Chandu

+0

私はこれを書いていますが、なぜこのshowReminderメソッドが呼び出されないのかわかりません – Chandu

+0

そのメソッドが呼び出しているかどうかをチェックしますか? – Tendulkar

4

YourViewController.mファイルに私たちはYourViewController.hファイル内のボタンのタッチで呼び出すと、その関数に身体を与える機能を追加

-(void)Trigger_LocalNotification 
{ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    UILocalNotification *_localNotification = [[UILocalNotification alloc]init]; 

     //setting the fire dat of the local notification 
    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; 


    //setting the time zone 
    _localNotification.timeZone = [NSTimeZone defaultTimeZone]; 

    //setting the message to display 
    _localNotification.alertBody = @"Did you forget something?"; 

    //default notification sound 
    _localNotification.soundName = UILocalNotificationDefaultSoundName; 

    //displaying the badge number 
    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1; 

    //schedule a notification at its specified time with the help of the app delegate 
    [[UIApplication sharedApplication]scheduleLocalNotification:_localNotification]; 

} 

最初の行コードが宣言されている場合、システムからのすべてのローカル通知が削除されます。 2行目では、UILocalNotification変数を初期化しています。3行目では、fireDateプロパティを使用してこのローカル通知がトリガーされる時刻を設定しています.5秒後に通知がトリガーされることがわかります。

soundNameは、通知がトリガーされたときにサウンドを再生するために使用されるUILocalNotificationクラスのプロパティで、このローカル通知を発生させたアプリがアクティブでない場合、アラートボックスにはデフォルトの通知がポップアップ表示されますalertBodyプロパティを使用してアラートメッセージが書き込まれます。コードの最後の行は、この通知をシステムに添付します。

今、あなたのプロジェクトのアプリケーションDelegate.mファイルを選択して、このクラス(YourViewController)のオブジェクトを作成 イベント

[btn addTarget:self action:@selector(Trigger_LocalNotification) forControlEvents:UIControlEventTouchUpInside]; 

の内側にタッチアップボタンでこの機能を添付してください

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    YourViewController *obj = [[YourViewController alloc]init]; 
    [self.window addSubview:obj.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

アプリケーションを実行し、シミュレータでアプリケーションを起動したら、すぐにホームボタンを押して警告ボックスを表示します5秒後にローカル通知の

この回答がUILocalNotificationの実装方法の学習に役立つことを願っています。

+0

アラートアクションはどうですか?私の場合、ユーザーがそのアクションボタンをクリックすると、アプリケーションはフォアグラウンドになり、ユーザーはアラート(アラートボディとアラートアクションのあるものではない)を表示する必要があります – Chandu

関連する問題