2015-03-25 26 views
1

如何在接收遠程通知和當我使用應用程序時顯示UILocalNotification?不是UIAlertView如何在接收遠程通知時顯示UILocalNotification?

我使用此代碼,但它沒有顯示任何

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    for (id key in userInfo) { 
     NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]); 
    } 


    localNotification = [[UILocalNotification alloc] init]; 
    NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
    localNotification.applicationIconBadgeNumber = 0; 
    localNotification.alertBody = message; 
    localNotification.soundName = UILocalNotificationDefaultSoundName; 
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.alertAction = @"ok!!!"; 
    [application scheduleLocalNotification:localNotification]; 
} 

我也試過:

[application presentLocalNotificationNow:localNotification]; 

但它不工作了。

任何人都可以幫忙嗎?

回答

2

Apple不支持從前臺狀態顯示本地通知。

Local Notification Reference

但是你可以嘗試使用這樣的:

https://github.com/avielg/AGPushNote

+0

雖然這可能在理論上回答這個問題,但[這將是更可取的](// meta.stackoverflow.com/q/8259)在這裏包括答案的基本部分,並提供供參考的鏈接。 – 2016-04-16 12:31:58

1

如果在通知下發的應用程序正在運行,則在屏幕上沒有顯示警報。應用程序自動調用其代表的應用程序:didReceiveLocalNotification:方法

-3

爲了在應用程序處於前臺時查看UI,您需要自己處理。

您可以從屏幕頂部製作自己的視圖並將其設置爲動畫,然後在延遲一段時間後再將其動畫化。

正如其他答案所提到的,有一些開放源代碼項目可以使這個更容易實現:除了AGPushNote我發佈了一個pod linked in this answer,它具有類似的功能。

+1

也請避免發佈多個答案,只是鏈接到您自己的產品,因爲它可能被視爲垃圾郵件。相反,定製每個問題的答案,並記得提供一個代碼示例,展示如何使用這個工具 – 2016-04-16 12:24:47

+0

是的,你已經挫敗了我的計劃,致力於麻煩MIT許可證。 ;) – buildsucceeded 2016-04-16 12:25:30

相關問題