當我的應用程序是不運行並接收推送通知,如果我點擊該通知,應用啓動 - 但是它不與提示用戶我建立了Alert-View,詢問他們是否想查看Notification的內容。它剛剛啓動,並坐在那裏。處理推送通知時,應用程序無法運行
的推送通知做的工作完美當應用程序是運行 - 無論是作爲活躍APP或者在後臺 - 但沒有正常工作時,應用程序是不運行。
我試圖註銷launchOptions
NSDictionary在application: didFinishLaunchingWithOptions:
看看是什麼加載它的帶來 - 但它出現爲「(空)」。所以它基本上不包含任何內容 - 這不合理,因爲它不應該包含通知的負載?
任何人有任何想法如何使推送通知的工作,當他們到達時,應用程序沒有運行?
編輯:這裏是我使用的application: didReceiveRemoteNotification
只是爲了看看什麼是什麼代碼:
if (UIApplicationStateBackground) {
NSLog(@"===========================");
NSLog(@"App was in BACKGROUND...");
}
else if (UIApplicationStateActive == TRUE) {
NSLog(@"===========================");
NSLog(@"App was ACTIVE");
}
else {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 99];
UIAlertView *BOOM = [[UIAlertView alloc] initWithTitle:@"BOOM"
message:@"app was INACTIVE"
delegate:self
cancelButtonTitle:@"a-ha!"
otherButtonTitles:nil];
[BOOM show];
NSLog(@"App was NOT ACTIVE");
}
所以這是應該採取應用程序的所有國家的照顧 - 但它不是。推送通知只適用於應用程序運行時 - 無論是在後臺還是在前臺...
======================== ========================
UPDATE/EDIT#2: 根據「@dianz」建議(下文)我修改了代碼我application: didFinishLaunchingWithOptions
的,包括以下內容:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *json = [localNotif valueForKey:@"data"];
UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"appDidFinishWithOptions"
message:json
delegate:self
cancelButtonTitle:@"cool"
otherButtonTitles:nil];
[bam show];
}
這確實讓AlertView框出現,但似乎沒有有效載荷:該AlertView的標題顯示出來(「appDidFinishWithOptions」),但JSON的NSString出現EMPTY ...將繼續調整...
======================
編輯#3 - 其現在的工作幾乎 100%
所以,在didFinishLaunchingWithOptions
:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
// Grab the pushKey:
pushKey = [localNotif valueForKey:@"pushKey"];
// "pushKey" is an NSString declared globally
// The "pushKey" in the "valueForKey" statement is part of my custom JSON Push Notification pay-load.
// The value of pushKey will always be a String, which will be the name of the
// screen I want the App to navigate to. So when a Notification arrives, I go
// through an IF statement and say "if pushKey='specials' then push the
// specialsViewController on, etc.
// Here, I parse the "alert" portion of my JSON Push-Notification:
NSDictionary *tempDict = [localNotif valueForKey:@"aps"];
NSString *pushMessage = [tempDict valueForKey:@"alert"];
// Finally, ask user if they wanna see the Push Notification or Cancel it:
UIAlertView *bam = [[UIAlertView alloc] initWithTitle:@"(from appDidFinishWithOptions)"
message:pushMessage
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"View Info", nil];
[bam show];
}
我接下來實現了alertView: clickedButtonAtIndex
方法,以查看用戶在AlertView上選擇的內容並據此進行操作。
這與邏輯didReceiveRemoteNotification
一起完美工作。然而,當應用程序沒有運行,並且我發送推送通知時,如果我一到它就不點擊推送通知警報,而是等待它淡出(發生這種情況在3-4秒之後),然後然後我點擊應用程序的圖標 - 該圖標現在的BADGE爲1 - 應用程序啓動,但在啓動時我沒有收到推送通知警報。它只是坐在那裏。
想我需要弄清楚是排列在下...
所以是你能得到通知時,你沒有點擊推送通知,並直接點擊應用程序圖標? –
你有這個工作。 ?我的意思是當應用程序處於非運行狀態時如何處理推送通知?如果如果您收到很多通知並且您沒有打開該應用程序,那麼您也不會點擊系統的通知面板。你如何保留這些推動以便以後檢索。 –
你碰巧得到最後的排列工作嗎? –