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

前端 未结 7 596
死守一世寂寞
死守一世寂寞 2020-12-12 15:45

I\'ve integrated push notifications in my app. Users will receive push notification to join a group. When the user clicks Join, I\'ve to handle something in

相关标签:
7条回答
  • 2020-12-12 16:12

    For anyone might be interested, I ended up creating a custom view that looks like the system push banner on the top but adds a close button (small blue X) and an option to tap the message for custom action. It also supports the case of more than one notification arrived before the user had time to read/dismiss the old ones (With no limit to how many can pile up...)

    Link to GitHub: AGPushNote

    The usage is basically on-liner:

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

    And it looks like this on iOS7 (iOS6 have an iOS6 look and feel...)

    Example

    0 讨论(0)
  • 2020-12-12 16:14

    The app will still receive the -application:didReceiveRemoteNotification message in your App Delegate, but you'll have to act on the message yourself (i.e. the alert isn't displayed by default).

    The userInfo parameter contains an object with the key notificationType, which you can use to identify the push message.

    0 讨论(0)
  • 2020-12-12 16:16

    I used code like this in my application delegate to mimic the notification alert when the app was active. You should implement the appropriate UIAlertViewDelegate protocol method(s) to handle what happen when the user taps either of the buttons.

    - (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
      }
    }
    
    0 讨论(0)
  • 2020-12-12 16:23

    You have to show the alert yourself if you want to. This is intentional behavior as documented here http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html below Listing 2-6

    0 讨论(0)
  • 2020-12-12 16:23

    For showing alert view while running application you have to use

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

    and by accessing the userInfo variable

    0 讨论(0)
  • 2020-12-12 16:33

    only this function will be invoked and you have to explicitly show the alert on that case no notification will come if app is running in which you have implement the notification.Put the break point there and handle the notification call when function called and show your customized alert there.

    0 讨论(0)
提交回复
热议问题