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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7965417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:09:17  来源:igfitidea点击:

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

iosobjective-ciphonepush-notificationuialertview

提问by Satyam

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:

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

- (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?

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

回答by Jakob W

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

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

回答by Aviel Gross

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...)

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

Link to GitHub: AGPushNote

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...)

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

Example

例子

回答by Ballu

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.

只有这个函数会被调用,你必须在这种情况下明确显示警报,如果应用程序正在运行,你已经实现了通知,则不会出现通知。在那里放置断点并在函数调用时处理通知调用并显示您的自定义警戒那里。

回答by Kerni

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.htmlbelow Listing 2-6

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

回答by Stark

For showing alert view while running application you have to use

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

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

and by accessing the userInfovariable

并通过访问userInfo变量

回答by Kristofer Sommestad

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

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

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

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

回答by Mike Zriel

Here is a version which support UIAlertController

这是一个支持 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 **

** 请注意,我的应用程序在 App Delegate 中使用 self.navigationController,只需连接到任何 ViewController 即可呈现(显示)警报 **