如果应用程序已在运行,如何处理推送通知? [英] How to handle push notifications if the application is already running?

查看:95
本文介绍了如果应用程序已在运行,如何处理推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

此外,如果我向APN发送有效负载,如何使用取消按钮创建警报?

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

推荐答案

您可以实施 application:didReceiveRemoteNotification:

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];
  }

这篇关于如果应用程序已在运行,如何处理推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆