iOS - 应用程序运行时不显示推送通知警报

发布于 2024-12-12 22:23:50 字数 331 浏览 0 评论 0原文

我已在我的应用程序中集成了推送通知。用户将收到加入群组的推送通知。当用户单击加入时,我必须处理代码中的某些内容。所以我正在实施:

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

当应用程序未运行时,这工作正常。
当应用程序运行时,我看不到任何 UIAlertView。如何让我的应用程序显示推送通知警报,以便用户仍然可以决定是否加入?

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 the code. And so I'm implementing:

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

This is working fine when the app is not running.
When the app is running, I don't see any UIAlertView. How can make my app show the push notification alert so that user can still decide whether to join or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

作死小能手 2024-12-19 22:23:50

我在应用程序委托中使用了这样的代码来模拟应用程序处于活动状态时的通知警报。您应该实现适当的 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
  }
}

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
  }
}
メ斷腸人バ 2024-12-19 22:23:50

对于任何可能感兴趣的人,我最终创建了一个自定义视图,看起来像顶部的系统推送横幅,但添加了一个关闭按钮(小蓝色 X)和一个用于点击消息以执行自定义操作的选项。它还支持在用户有时间阅读/忽略旧通知之前到达多个通知的情况(对可以堆积的数量没有限制......)

GitHub 链接:AGPushNote

使用方式基本上是在线的:

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

在 iOS7 上看起来像这样(iOS6 有 iOS6 的外观和感觉。 ..)

示例

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

吹梦到西洲 2024-12-19 22:23:50

如果您愿意,您必须自己显示警报。这是故意行为,如此处记录的 http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html 下面清单 2-6

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

巷子口的你 2024-12-19 22:23:50

仅调用此函数,并且您必须在这种情况下显式显示警报,如果您已实现通知的应用程序正在运行,则不会收到任何通知。将断点放在那里,并在调用函数时处理通知调用并显示您的自定义在那里保持警惕。

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.

风蛊 2024-12-19 22:23:50

要在运行应用程序时显示警报视图,您必须使用

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

并访问 userInfo 变量

For showing alert view while running application you have to use

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

and by accessing the userInfo variable

灵芸 2024-12-19 22:23:50

应用程序仍会在您的应用程序委托中接收 -application:didReceiveRemoteNotification 消息,但您必须自己对该消息采取行动(即默认情况下不显示警报)。

userInfo 参数包含一个带有键notificationType 的对象,您可以使用它来标识推送消息。

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.

与风相奔跑 2024-12-19 22:23:50

这是支持 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 即可呈现(显示)警报 **

Here is a version which support 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];

}

}

** Please note my app uses self.navigationController in App Delegate, just hook on to any ViewController to present ( show ) the Alert **

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文