1

我可以將推送通知發送到我的IOS設備。但是當我點擊該通知時,它只是打開應用程序。應用程序內沒有顯示任何消息。由我使用當應用程序未運行時處理推送通知(即完全死亡)

代碼:

if (application.applicationState == UIApplicationStateActive) { 

    NSString *cancelTitle = @"Close"; 

    NSString *showTitle = @"Show"; 

    NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title" 

                 message:message 

                 delegate:self 

               cancelButtonTitle:cancelTitle 

               otherButtonTitles:showTitle, nil]; 

    [alertView show]; 

    [alertView release]; 

} else { 

    //Do stuff that you would do if the application was not active 

} 

但無法顯示我的消息,以上代碼的幫助。上述代碼僅適用於我的應用程序處於前臺狀態的情況下打開,而不是僅此警報纔會顯示。

請幫忙。

謝謝。

+0

,你必須把你剛纔notification.The代碼是不相關的代碼顯示警報查看應用何時處於活動狀態。你想做什麼 ? –

+0

我在我的IOS設備上收到推送通知。我只是想當我點擊推送消息,而不是它應該打開我的應用程序,並顯示該消息給用戶多數民衆贊成它。如果您使用正確的代碼詳細指導,會很高興。 @Teja Nandamuri –

回答

-2

處理這種事情的最好方法是在APN中使用深度鏈接。這將讓您嵌入數據,然後可以在您的應用程序中處理數據,並將用戶引導至特定事件。

否則,您僅限於使用應用程序委託中的ApplicationDidEnterForeground方法。只要把你的alertView代碼放在那裏,任何時候你的應用程序都會被放到前臺運行。

+0

能否請您提供一個解決方案詳細的代碼@TheValyreanGroup –

+0

這裏是一個很好的寫在如何做到這一點。您需要3件事... 1-Custom URL Scheme APN內的2-Deep鏈接 3-Code用於處理來自APN的深層鏈接 https://blog.mixpanel.com/2015/04/21/ guide-setting-up-deep-linking-in-ios-and-android/ – TheValyreanGroup

+0

感謝您的幫助,但該教程提供瞭如何使用深層鏈接打開特定的推送通知屏幕的知識。但我只想在主屏幕上顯示我的推送通知。 @TheValyreanGroup –

1

從蘋果文檔

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/Introduction.html

「當你的應用程序必須啓動以 收到通知,UIKit中包括推出 UIApplicationLaunchOptionsLocalNotificationKey或 UIApplicationLaunchOptionsRemoteNotificationKey關鍵 選項字典傳遞給你的應用程序代理的 應用程序:willFinishLaunchingWithOptions:和 應用程序:didFinishLaunchingWithOptio ns:方法。 這些密鑰的存在讓你知道,有通知數據等待 處理,並讓你有機會適當地配置你的應用程序的界面 。不過,您不需要在這些 方法中處理通知。在您的應用運行後,UIKit會調用您的應用委託的其他方法 ,例如 應用:didReceiveLocalNotification:方法,以便爲您提供處理通知數據的 機會。這方法被稱爲 取決於哪些方法您實現與用戶 是否與該消息的系統UI交互。」

所以,請檢查您的應用程序已經啓動,由於通知,如果是這樣顯示的對話框

0

我認爲有一個簡單的解決方案。

  1. 可以存儲一些標誌的應用程序,一旦當您收到 通知,您可以檢查該怎麼辦呢here
  2. 後,關於你當你從後臺 也許像here
  3. 檢測

如果你想,當你從通知打開應用程序,只顯示警報,那麼也許你這是你solution

+0

50%我有一個解決方案。使用這個代碼:else if(application。應用程序狀態== UIApplicationStateInactive){ //應用程序正在從後臺切換到前臺(用戶點擊通知),當用戶點擊此處時執行所需操作 }但是,如果我用於UiapplicationStateBackground,它對我不起作用。 @ m1sh0 –

1

當應用程序未運行(或完全死亡)時處理推送通知

我發佈此解決方案,因爲它爲我工作。

轉到您的AppDelegate.m文件。

步驟1: 寫入這個函數內的代碼:

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

    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 

if (localNotif) { 

     NSString *cancelTitle = @"Close"; 
     NSString *showTitle = @"OK"; 
     NSString *message = [[localNotif valueForKey:@"aps"] valueForKey:@"alert"]; 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:showTitle, nil]; 
     [alertView show]; 


    } 

} 

步驟2:

插入該全碼:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 

NSLog(@"%s..userInfo=%@",__FUNCTION__,userInfo); 

/** 
    * Dump your code here according to your requirement after receiving push 
    */ 

    if (application.applicationState == UIApplicationStateActive) { 
     NSString *cancelTitle = @"Close"; 
     NSString *showTitle = @"OK"; 
     NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:showTitle, nil]; 
     [alertView show]; 

    } 

    else if(application.applicationState == UIApplicationStateBackground){ 

     //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here 


     NSString *cancelTitle = @"Close"; 
     NSString *showTitle = @"OK"; 
     NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:showTitle, nil]; 
     [alertView show]; 

    } 


    else if(application.applicationState == UIApplicationStateInactive){ 

     //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here 


     NSString *cancelTitle = @"Close"; 
     NSString *showTitle = @"OK"; 
     NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"]; 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message Received" 
                  message:message 
                  delegate:self 
                cancelButtonTitle:cancelTitle 
                otherButtonTitles:showTitle, nil]; 
     [alertView show]; 

    } 


} 

這整個代碼將工作,無論是應用程序有效,無效或完全死亡。它會爲您提供推送消息的AlertView。

1

當應用程序被完全殺死GET通知代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
if (launchOptions != nil) 
{ 
    //opened from a push notification when the app is closed 
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    if (userInfo != nil) 
    { 
     NSLog(@"userInfo->%@",[userInfo objectForKey:@"aps"]); 
     //write you push handle code here 

    } 

} 
} 

更多通過這個鏈接:Handling Push Notifications when App is Terminated

相關問題