如果应用程序已经在运行,如何处理推送通知?

发布于 2024-08-08 02:19:47 字数 122 浏览 4 评论 0原文

如果应用程序已经在运行,我们如何处理推送通知?我想在应用程序正在运行时显示警报(而不是推送通知警报)。仅当应用程序未运行时,才显示推送通知警报。

另外,如果我向 APNs 发送有效负载,如何创建带有取消按钮的警报?

How do we handle push notifications if the application is already running ? I want to show an alert if the application is running (instead of a push notification alert). Only if the application is not running, then show a push notification alert.

Also, if I send a payload to APNs, how can I create an alert with a cancel button?

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

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

发布评论

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

评论(4

情归归情 2024-08-15 02:19:47

您可以实现 application:didReceiveRemoteNotification:

这是一个可能的示例代码:

- (void)application:(UIApplication *)application
   didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  NSString *message = nil;
  id alert = [userInfo objectForKey:@"alert"];
  if ([alert isKindOfClass:[NSString class]]) {
    message = alert;
  } else if ([alert isKindOfClass:[NSDictionary class]]) {
    message = [alert objectForKey:@"body"];
  }
  if (alert) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                       message:@"AThe message."  delegate:self
                             cancelButtonTitle:@"button 1"
                             otherButtonTitles:@"button", nil];
    [alertView show];
    [alertView release];
  }

You can implement application:didReceiveRemoteNotification:

Here is a possible sample code:

- (void)application:(UIApplication *)application
   didReceiveRemoteNotification:(NSDictionary *)userInfo
{
  NSString *message = nil;
  id alert = [userInfo objectForKey:@"alert"];
  if ([alert isKindOfClass:[NSString class]]) {
    message = alert;
  } else if ([alert isKindOfClass:[NSDictionary class]]) {
    message = [alert objectForKey:@"body"];
  }
  if (alert) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                       message:@"AThe message."  delegate:self
                             cancelButtonTitle:@"button 1"
                             otherButtonTitles:@"button", nil];
    [alertView show];
    [alertView release];
  }
深空失忆 2024-08-15 02:19:47

您可以检查 UIApplication 的状态。只需像这样进行检查

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

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive)
    {

            UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"xxx" message:yourMessage delegate:self cancelButtonTitle:@"Done" otherButtonTitles: @"Anzeigen", nil] autorelease];
            [alert setTag: 2];
            [alert show];
    }
    else {
        // Push Notification received in the background
    }
}

You can check for the state of the UIApplication. Just do a check like this

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

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive)
    {

            UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"xxx" message:yourMessage delegate:self cancelButtonTitle:@"Done" otherButtonTitles: @"Anzeigen", nil] autorelease];
            [alert setTag: 2];
            [alert show];
    }
    else {
        // Push Notification received in the background
    }
}
琴流音 2024-08-15 02:19:47

“alert”键不会直接位于 userInfo 字典下,您需要获取另一个名为“aps”的字典,然后从“aps”字典中获取“alert”或“body”。

"alert" key will not be there directly under the userInfo dictionary, you need to get another dictionary with name "aps" and then get the "alert" or "body" from "aps" dictionary.

陈甜 2024-08-15 02:19:47

迭代 3 个级别的有效负载

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

for (id key in userInfo) {
    NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    NSString *message = nil;

NSDictionary *aps = [NSDictionary dictionaryWithDictionary:(NSDictionary *) [userInfo objectForKey:key] ];   
    for (id key1 in aps){
         NSLog(@"key1: %@", key1);
        id alert = [aps objectForKey:key1];
        if ([alert isKindOfClass:[NSDictionary class]]) {
            message = [alert objectForKey:@"body"];
             NSLog(@"body: %@, value: %@", key1, message);
            message = [alert objectForKey:@"loc-args"];
            NSLog(@"loc-args: %@, value: %@", key1, message);
            NSArray *args = (NSArray *) [alert objectForKey:@"loc-args"] ;
                for (id key2 in args){
                    NSLog(@"key2: %@, value: ", key2);
                }
            message = [alert objectForKey:@"action-loc-key"];
            NSLog(@"action-loc-key: %@, value: %@", key1, message);

        }
        else if ([alert isKindOfClass:[NSArray class]]) {
            for (id key2 in key1){
                NSLog(@"key2: %@, value: %@", key2, [key1 objectForKey:key2]);
            }
        }
        else if([key1 isKindOfClass:[NSString class]]) {
            message = [aps objectForKey:key1];
            NSLog(@"key1: %@, value: %@", key1, message);
        } 

    }
  } 

}

结果是:

2012-01-27 20:38:09.599 SPush[4181:707] key: aps, value: {
alert =     {
    "action-loc-key" = Open;
    body = test;
    "loc-args" =         (
        1000,
        2000
    );
};
badge = 0;
"content-available" = 10;
sound = default;
}
2012-01-27 20:38:13.133 SPush[4181:707] key1: alert
2012-01-27 20:38:13.134 SPush[4181:707] body: alert, value: test
2012-01-27 20:38:13.137 SPush[4181:707] loc-args: alert, value: (
1000,
2000
)
2012-01-27 20:38:13.138 SPush[4181:707] key2: 1000, value: 
2012-01-27 20:38:13.139 SPush[4181:707] key2: 2000, value: 
2012-01-27 20:38:13.140 SPush[4181:707] action-loc-key: alert, value: Open
2012-01-27 20:38:13.141 SPush[4181:707] key1: sound
2012-01-27 20:38:13.143 SPush[4181:707] key1: sound, value: default
2012-01-27 20:38:13.144 SPush[4181:707] key1: badge
2012-01-27 20:38:13.145 SPush[4181:707] key1: badge, value: 0
2012-01-27 20:38:13.146 SPush[4181:707] key1: content-available
2012-01-27 20:38:13.147 SPush[4181:707] key1: content-available, value: 10

Iterating the 3 levels of payload

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

for (id key in userInfo) {
    NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    NSString *message = nil;

NSDictionary *aps = [NSDictionary dictionaryWithDictionary:(NSDictionary *) [userInfo objectForKey:key] ];   
    for (id key1 in aps){
         NSLog(@"key1: %@", key1);
        id alert = [aps objectForKey:key1];
        if ([alert isKindOfClass:[NSDictionary class]]) {
            message = [alert objectForKey:@"body"];
             NSLog(@"body: %@, value: %@", key1, message);
            message = [alert objectForKey:@"loc-args"];
            NSLog(@"loc-args: %@, value: %@", key1, message);
            NSArray *args = (NSArray *) [alert objectForKey:@"loc-args"] ;
                for (id key2 in args){
                    NSLog(@"key2: %@, value: ", key2);
                }
            message = [alert objectForKey:@"action-loc-key"];
            NSLog(@"action-loc-key: %@, value: %@", key1, message);

        }
        else if ([alert isKindOfClass:[NSArray class]]) {
            for (id key2 in key1){
                NSLog(@"key2: %@, value: %@", key2, [key1 objectForKey:key2]);
            }
        }
        else if([key1 isKindOfClass:[NSString class]]) {
            message = [aps objectForKey:key1];
            NSLog(@"key1: %@, value: %@", key1, message);
        } 

    }
  } 

}

The result is:

2012-01-27 20:38:09.599 SPush[4181:707] key: aps, value: {
alert =     {
    "action-loc-key" = Open;
    body = test;
    "loc-args" =         (
        1000,
        2000
    );
};
badge = 0;
"content-available" = 10;
sound = default;
}
2012-01-27 20:38:13.133 SPush[4181:707] key1: alert
2012-01-27 20:38:13.134 SPush[4181:707] body: alert, value: test
2012-01-27 20:38:13.137 SPush[4181:707] loc-args: alert, value: (
1000,
2000
)
2012-01-27 20:38:13.138 SPush[4181:707] key2: 1000, value: 
2012-01-27 20:38:13.139 SPush[4181:707] key2: 2000, value: 
2012-01-27 20:38:13.140 SPush[4181:707] action-loc-key: alert, value: Open
2012-01-27 20:38:13.141 SPush[4181:707] key1: sound
2012-01-27 20:38:13.143 SPush[4181:707] key1: sound, value: default
2012-01-27 20:38:13.144 SPush[4181:707] key1: badge
2012-01-27 20:38:13.145 SPush[4181:707] key1: badge, value: 0
2012-01-27 20:38:13.146 SPush[4181:707] key1: content-available
2012-01-27 20:38:13.147 SPush[4181:707] key1: content-available, value: 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文