簡體   English   中英

iOS-應用程序運行時不顯示推送通知警報

[英]iOS - Push notification alert is not shown when the app is running

我已經在應用程序中集成了推送通知。 用戶將收到推送通知以加入群組。 當用戶單擊Join時 ,我必須處理代碼中的某些內容。 因此,我正在實施:

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

當應用未運行時,這可以正常工作。
當應用運行時,我看不到任何UIAlertView 如何使我的應用顯示推送通知警報,以便用戶仍可以決定是否加入?

我在應用程序委托中使用了這樣的代碼來模仿應用程序處於活動狀態時的通知警報。 您應該實現適當的UIAlertViewDelegate協議方法,以處理用戶點擊任意一個按鈕時發生的情況。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {    
  UIApplicationState state = [application applicationState];
  if (state == 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
  }
}

對於可能感興趣的任何人,我最終創建了一個自定義視圖,該視圖看起來像頂部的系統推送橫幅,但是添加了一個關閉按鈕(小藍色X)和一個點擊消息以執行自定義操作的選項。 它還支持在用戶有時間讀取/關閉舊通知之前到達多個通知的情況(不限制可以堆積多少個…)

鏈接到GitHub:AGPushNote

用法基本上是在線的:

[AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];

在iOS7上看起來像這樣(iOS6具有iOS6外觀...)

例

如果需要,您必須自己顯示警報。 如清單2-6所示,這是此處http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html所記錄的故意行為。

要在運行應用程序時顯示警報視圖,您必須使用

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

並通過訪問userInfo變量

僅此函數將被調用,並且在這種情況下您必須顯式顯示警報,如果運行應用了通知的應用正在運行,則不會發出通知。將斷點放在此處並在函數調用時處理通知調用並顯示您的自定義在那里提醒。

這是支持UIAlertController的版本

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if (state == UIApplicationStateActive) {

    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:notification.alertTitle
                                  message:notification.alertBody
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             [alert dismissViewControllerAnimated:YES completion:nil];

                         }];

    [alert addAction:ok];

    [self.navigationController presentViewController:alert animated:YES completion:nil];

}

}

**請注意,我的應用程序在App Delegate中使用self.navigationController,只需掛接到任何ViewController上即可顯示(顯示)警報**

該應用程序仍會在您的應用程序委托中收到-application:didReceiveRemoteNotification消息,但是您必須自己對消息進行操作(即默認情況下不顯示警報)。

userInfo參數包含一個具有鍵notificationType的對象,可用於標識推送消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM