2011-07-28 34 views

回答

8

不,您將收到appdelegate中的通知。

- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification { 
    //Place your code to handle the notification here. 
} 
+0

非常感謝。現在,我在應用程序didReceiveLocalNotification方法中保留了一個UIAlertView,以便在應用程序已經運行時可以使用它來代替通知。但是當應用程序處於後臺並且通知被觸發並且應用程序到達前臺時,將調用此方法,並顯示alertView。你能告訴我如何避免這種情況嗎? –

+0

好的,我們有applicationWillEnterForeground:方法。對不起,愚蠢的問題!非常感謝。 –

+0

不是一個愚蠢的問題 - 你只是給了我正在尋找的答案:-) –

0

如果您的應用程序是目前在前臺下面的函數將被調用你的委託:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)Notifikation 

然後你可以決定顯示的alertview,但其本身的標準一個不會出現

+1

無論應用程序是否已在運行,都會調用它。 –

+0

只有當用戶點擊「打開」按鈕時... – Bastian

3

我做了一個lib,使動畫幾乎與本地通知一樣。

檢查: https://github.com/OpenFibers/OTNotification

演示: enter image description here

enter image description here

而當你在

- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification 
{ 
    OTNotificationManager *notificationManager = [OTNotificationManager defaultManager]; 
    OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init]; 
    notificationMessage.title = [self notificationTitle]; 
    notificationMessage.message = @"A notification. Touch me to hide me."; 
    [notificationManager postNotificationMessage:notificationMessage]; 
} 
2

接收到的消息可以發佈一個新的消息,這LIB接受的anser是正確的,但它不夠○接收所有通知,並顯示一些用戶從

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

你必須檢查,是當前的通知與否。 有時會發生另一個通知(例如,當您取消它們時)。所以,你必須檢查,那是你除了:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { 
    if (fabs([[NSDate date] timeIntervalSinceDate:[notification fireDate]]) <= 0.5f) 
    { 
     [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notification alert", @"") 
            message:notification.alertBody 
            delegate:self 
          cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];  
    } 
} 
0

雨燕2.2:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) { 
    var state = application.applicationState 
    if state == .Active { 
     // handle the notification, e.g. show an alert 
    } 
} 

雨燕3.0:

func application(_ application: UIApplication, didReceive notification: UILocalNotification) { 
    var state: UIApplicationState = application.applicationState 
    if state == .active { 
     // handle the notification, e.g. show an alert 
    } 
}