应用程序在后台时如何处理 iOS 远程通知

2024-06-24

我正在通过苹果推送通知开发iOS推送通知功能,现在当我的应用程序处于后台或前台时我收到了正确的通知,但我想当我的应用程序处于后台时基本上当我的应用程序处于后台时处理远程通知只是显示来自有效负载的警报消息。实际上我只是想自定义我的远程通知。

code:

 - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
//        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
return YES;
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", error);
}
- (void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken {
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken);
}
-(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (UIBackgroundFetchResult))completionHandler
{
NSDictionary * aps=[userInfo valueForKey"aps"];
NSLog(@"did recevie %@",aps);
NSLog(@"userinfo details %@",[aps valueForKey"alert"]);
}

在 iOS 10 中首先你必须设置UNUserNotificationCenterDelegate in AppDelegate.h file

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,UNUserNotificationCenterDelegate>

之后在AppDelegate.m像这样写代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.1) {
            // iOS 7.1 or earlier. Disable the deprecation warnings.
            UIRemoteNotificationType allNotificationTypes =
            (UIRemoteNotificationTypeSound |
             UIRemoteNotificationTypeAlert |
             UIRemoteNotificationTypeBadge);
            [application registerForRemoteNotificationTypes:allNotificationTypes];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        } else {
            // iOS 8 or later
            // [START register_for_notifications]
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
    {
                UIUserNotificationType allNotificationTypes =
                (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
                UIUserNotificationSettings *settings =
                [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
                [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
                [[UIApplication sharedApplication] registerForRemoteNotifications];
                [application registerForRemoteNotifications];
            } else {
                // iOS 10 or later
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
                UNAuthorizationOptions authOptions =
                UNAuthorizationOptionAlert
                | UNAuthorizationOptionSound
                | UNAuthorizationOptionBadge;
                [[UNUserNotificationCenter currentNotificationCenter]
                 requestAuthorizationWithOptions:authOptions
                 completionHandler:^(BOOL granted, NSError * _Nullable error) {
                 }
                 ];
                // For iOS 10 display notification (sent via APNS)
                [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];            
    [[UIApplication sharedApplication] registerForRemoteNotifications];
         return YES;
}

现在iOS10以下版本实现此方法

    - (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
        NSLog(@"Notification received: %@", userInfo);
        if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )
        {
            NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
            return;
        }
        NSLog( @"HANDLE PUSH, didReceiveRemoteNotification: %@", userInfo );
          else{
            handler( UIBackgroundFetchResultNewData );
}

    }

苹果在iOS10中引入了这两种方法来接收推送通知。

也写这些方法

    // Receive displayed notifications for iOS 10 devices.
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
           willPresentNotification:(UNNotification *)notification
             withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

        NSDictionary *userInfo = notification.request.content.userInfo;

        NSLog(@"%@", userInfo);

            completionHandler( UNNotificationPresentationOptionAlert );
   }

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

    NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
//    completionHandler(UNNotificationPresentationOptionAlert);
       }

就是这样。

Try this

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

应用程序在后台时如何处理 iOS 远程通知 的相关文章

随机推荐

  • Angularjs 切换 div 可见性

    我试图通过单击按钮来切换 div 文本 我尝试采用范围变量并根据该变量切换类名 我在哪里犯了错误here http jsfiddle net nw5ndzrt 340
  • 选择所有复选框/行 - React hooks 应用程序

    我正在努力一次勾选表格中的所有复选框 我可以检查各个复选框 但我也想要检查的选项 一个 主复选框 用于检查所有其他复选框 我创建了 React 组件的简化版本 https codesandbox io s lively wildflower
  • Windows 服务的 Log4net

    这是我第一次使用Log4net 它没有显示任何错误 但没有写入文件 我在我的中添加了以下内容AppConfig file
  • 如何将文件包含在 php 中后排除它?

    我们在里面添加一个文件之后Myfile php with require once abc php or include once abc php 我们将如何删除该文件 或者如何停止abc php在特定代码块之后访问文件内容 在 PHP 中
  • RtaskscheduleR不执行脚本

    我正在尝试使用每分钟运行一次脚本taskscheduleR图书馆 我按照 GitHub 页面上的示例进行操作 但遇到了以下问题 R 表示任务已创建 但脚本未正确执行 它应该以附加模式将数据写入文件 有问题的文件已存在 我没有看到任何日志文件
  • QSettings - ini 文件的位置在哪里?

    我在用着QSettings在 Windows 中将一些数据存储为 ini 文件 我想查看ini文件 但我不知道ini文件的位置在哪里 这是我的代码 QSettings set new QSettings QSettings IniForma
  • 维基百科 API 搜索标题生成器

    尝试使用生成器通过 api 搜索图块 我注意到有两种可能的生成器 但我都遇到了问题 前缀搜索 如果我有多个单词并且查询中的顺序相反 例如 brian adams 将返回答案 但 adams brian 则不会 则效果不佳 搜索 似乎不允许按
  • RapidXML 打印标头具有未定义的方法

    我一直在我的一个项目中使用 RapidXML 一切都很顺利 直到我决定使用它来编写 xml 我的代码或多或少如下 attempt to open the file for writing std ofstream file fileName
  • 如何对相同值的范围进行二分查找?

    我有一个已排序的数字列表 我需要让它返回该数字出现的索引范围 我的清单是 daysSick 0 0 0 0 1 2 3 3 3 4 5 5 5 6 6 11 15 24 如果我搜索 0 我需要返回 0 3 现在我只能找到一个数字的位置 我知
  • 具有 ISO 日期格式的 WCF REST JSON

    我在 WCF Web 服务 框架 4 0 中使用 JSON 支持 ISO 日期格式时遇到了一个大问题 我尝试了很多 但还没有运气 休息服务 WebInvoke Method PUT UriTemplate mvnoid OrderID Re
  • WPF 找不到某些字体

    为什么会这样Media Fonts找不到 Arial Rounded MT Bold foreach var f in System Windows Media Fonts SystemFontFamilies if f Source Ar
  • VS2013 - 如何使用外部程序在 C# 项目的命令行参数中传递解决方案文件夹 $(SolutionDir)

    我正在为 Excel 构建一个 C 添加 为了调试它 我需要使用包含插件的调试或发布路径的命令行参数启动 Excel exe 例如 启动外部程序 C Program Files Microsoft Office Office15 EXCEL
  • 在python中缓存数据库数据

    在项目中 我需要缓存从每个请求获取的数据库数据 这样从下次开始 将从缓存而不是数据库中选取数据 表行 从而提高性能 在数据库中 我有超过 10M 的数据行 我正在浏览烧杯缓存文档 从中看来它只会将函数和参数一起缓存为键 那么这如何存储我的主
  • JFreeChart:如何更改XYPlot前景色?

    JFreeChart XYPlot 背景颜色更改为setBackgroundPaint 但好像没有对应的setForegroundPaint XYPlot plot XYPlot chart getPlot plot setBackgrou
  • 如何向heroku节点服务器添加python依赖项?

    我有一个运行 Node 的 Heroku 应用程序 但我需要能够在此服务器上运行 Python 脚本 我正在尝试安装我的 Python 依赖项 但无法让它工作 我已将 python 和节点构建包添加到我的项目中 我已创建虚拟环境并成功安装了
  • .Net 中的 Midi 实现

    有人对在 C Winforms 中编写基于 MIDI 的应用程序有任何指导或建议吗 我最近购买了一个新的效果踏板 它具有完整的 MIDI 实现 或者我相信 但制造商认为不发布图书馆员 补丁编辑应用程序是合适的 除了将键盘插入另一个 MIDI
  • 启动包中没有指定postgresql用户名

    public class HelloPostgreSQLActivity extends Activity TextView resultArea Override public void onCreate Bundle savedInst
  • Erlang:远程调用与发送消息

    我想在远程节点上执行一些过程 我不确定哪种方法是最好的方法 我可以写一个rpc call去做这个 或通过以下方式发送消息Remote call some procedure 到节点来启动程序并使用receive等待回复 那么erlang中哪
  • 将数据从 Django 传递到 D3

    我正在尝试使用 Django 和 D3 js 编写一个非常基本的条形图 我有一个名为 play 的对象 其中包含一个名为 date 的日期时间字段 我想要做的是显示一段时间内按月分组的播放次数 基本上我有两个问题 如何将这些内容按月分组并统
  • 应用程序在后台时如何处理 iOS 远程通知

    我正在通过苹果推送通知开发iOS推送通知功能 现在当我的应用程序处于后台或前台时我收到了正确的通知 但我想当我的应用程序处于后台时基本上当我的应用程序处于后台时处理远程通知只是显示来自有效负载的警报消息 实际上我只是想自定义我的远程通知 c