2014-10-07 55 views
0

我在我的應用程序上使用本地通知。當它到達時,我會完美地處理通知,並且即使應用程序運行甚至沒有運行,並且當您從設備的「通知」中打開通知時,也會按通知。但是,當應用程序沒有運行,你打開直接的應用程序,然後通知屏幕不會打開,徽章號碼不會是0.我錯過了什麼?目標c的iOS本地通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
     UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
if (locationNotification) { 
    UILocalNotification *localNotif = 
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    NotificationsViewController *vc = nil; 
    if (localNotif) { 
     //base on notification create right view controller 
     vc = [[NotificationsViewController alloc] init]; 
     NSLog(@"Recieved Notification %@",localNotif); 
    } 
    else 
    { 
     //create default view controller 
     vc = [[NotificationsViewController alloc] init]; 
    } 

    // Add the view controller's view to the window and display. 
    _window.rootViewController = vc; 
    [_window makeKeyAndVisible]; 

    // Set icon badge number to zero 
    application.applicationIconBadgeNumber = 0; 
} 

return YES; 
} 




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

NotificationsViewController *vc = nil; 

vc = [[NotificationsViewController alloc] init]; 
_window.rootViewController = vc; 
// Set icon badge number to zero 
application.applicationIconBadgeNumber = 0; 

//We take the rootViewController first which is expected to be a UINavigationController in your case 

    } 
+1

如果您沒有從通知中打開應用程序,該應用程序不知道有通知 – jcesarmobile 2014-10-07 07:54:28

+0

檢查applicationIconBadgeNumber如何?我做了這樣的事情,它似乎工作,但我想不是最好的辦法:if(application.applicationIconBadgeNumber> 0) NotificationsViewController * vc = nil; vc = [[NotificationsViewController alloc] init]; _window.rootViewController = vc; [_window makeKeyAndVisible]; //將圖標徽章號碼設置爲零 application.applicationIconBadgeNumber = 0; } – Katerina 2014-10-07 08:23:40

+0

與你可以知道是否有通知,但你不能得到通知信息(消息或其他數據發送) – jcesarmobile 2014-10-07 08:45:40

回答

0

,如果你不從通知打開應用程序,應用程序不知道有通知

檢查applicationIconBadgeNumber就可以知道是否有一個通知,但你不能得到通知信息(您發送郵件或其他數據)

0

當你的應用程序沒有運行 如果「didFinishLaunchingWithOptions」將被稱爲不didReceiveLocalNotification,你應該跟蹤參數launchOptions檢測啓動模式: 如果應用程序開始從直接應用圖標,你應該不會將用戶重定向到通知屏幕。 如果應用程序從NSNotification開始,你應該重定向用戶通知,計劃通知,更新應用程序圖標徽章等...

所以在這種情況下,當你從應用程序圖標locationNotification打開應用程序可能爲零。