应用程序首次启动时调用 didReceiveRemoteNotification

2024-03-01

我已经实现了 didReceiveRemoteNotification 方法。它工作并显示一个视图控制器以及传递的通知数据。仅当应用程序已在前台或在后台运行时,此功能才有效。但是,当应用程序未运行并且用户单击通知时,应用程序会启动,但看起来好像没有收到通知。通知不会写入文本文件,并且视图控制器不会被推送。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive)
    {
        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        NSString *alertMsg = @"";
        NSString *badge = @"";
        NSString *sound = @"";
        NSString *custom = @"";

        if( [apsInfo objectForKey:@"alert"] != NULL)
        {
            alertMsg = [apsInfo objectForKey:@"alert"];
        }


        if( [apsInfo objectForKey:@"badge"] != NULL)
        {
            badge = [apsInfo objectForKey:@"badge"];
        }


        if( [apsInfo objectForKey:@"sound"] != NULL)
        {
            sound = [apsInfo objectForKey:@"sound"];
        }

        if( [userInfo objectForKey:@"Type"] != NULL)
        {
            custom = [userInfo objectForKey:@"Type"];
        }

        // Set your appending text.
        NSString *textToAdd = [NSString stringWithFormat:@":%@", alertMsg];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *fileContents = [[NSString alloc]  initWithContentsOfFile:fileName usedEncoding:nil error:nil];

        NSString *textToFile;

        if (fileContents == NULL)
        {
            textToFile = alertMsg;
        }

        // Here you append new text to the existing one
        if (fileContents != NULL)
        {
            textToFile = [fileContents stringByAppendingString:textToAdd];
        }

        // Here you save the updated text to that file
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *content = textToFile;
        [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

        NSArray *fileData = [textToFile componentsSeparatedByString:@":"];

        NSMutableArray *tableDataFromFile;
        tableDataFromFile = [[NSMutableArray alloc] init];

        int i = 0;

        for (i = 1; i < [fileData count]; i++)
        {
            [tableDataFromFile addObject:fileData[i]];
        }

        NotificationViewController *vc = [[NotificationViewController alloc] initWithNibName:@"NotificationViewController" bundle:nil];
        vc.tableData = tableDataFromFile;

        UIViewController *root = self.mainNavController.topViewController;
        NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
        [self.mainNavController setViewControllers:vcs animated:YES];
    }
        // app was already in the foreground
    else
    {
        while (done == FALSE)
        {

        }

        NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

        NSString *alertMsg = @"";
        NSString *badge = @"";
        NSString *sound = @"";
        NSString *custom = @"";

        if( [apsInfo objectForKey:@"alert"] != NULL)
        {
            alertMsg = [apsInfo objectForKey:@"alert"];
        }


        if( [apsInfo objectForKey:@"badge"] != NULL)
        {
            badge = [apsInfo objectForKey:@"badge"];
        }


        if( [apsInfo objectForKey:@"sound"] != NULL)
        {
            sound = [apsInfo objectForKey:@"sound"];
        }

        if( [userInfo objectForKey:@"Type"] != NULL)
        {
            custom = [userInfo objectForKey:@"Type"];
        }

        // Set your appending text.
        NSString *textToAdd = [NSString stringWithFormat:@":%@", alertMsg];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *fileContents = [[NSString alloc]  initWithContentsOfFile:fileName usedEncoding:nil error:nil];

        NSString *textToFile;

        if (fileContents == NULL)
        {
            textToFile = alertMsg;
        }

        // Here you append new text to the existing one
        if (fileContents != NULL)
        {
            textToFile = [fileContents stringByAppendingString:textToAdd];
        }

        // Here you save the updated text to that file
        paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        documentsDirectory = [paths objectAtIndex:0];
        fileName = [NSString stringWithFormat:@"%@/AccountNotifications.txt", documentsDirectory];
        NSString *content = textToFile;
        [content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

        NSArray *fileData = [textToFile componentsSeparatedByString:@":"];

        NSMutableArray *tableDataFromFile;
        tableDataFromFile = [[NSMutableArray alloc] init];

        int i = 0;

        for (i = 1; i < [fileData count]; i++)
        {
            [tableDataFromFile addObject:fileData[i]];
        }

        NotificationViewController *vc = [[NotificationViewController alloc] initWithNibName:@"NotificationViewController" bundle:nil];
        vc.tableData = tableDataFromFile;

        UIViewController *root = self.mainNavController.topViewController;
        NSArray *vcs = [NSArray arrayWithObjects:root, vc, nil];
        [self.mainNavController setViewControllers:vcs animated:YES];

    }
            // app was just brought from background to foreground


}

有人可以帮我解决这个问题吗?一旦 didFinishLaunchingWithOptions 完成,布尔值 did 就会设置为 true。我只希望如果在应用程序根本没有运行时按下通知,则 notificationviewcontroller 打开并显示通知。


您应该在代码中添加这样的内容:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
    *)launchOptions {

        NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

        //Accept push notification when app is not open
        if (remoteNotif) {      
            [self handleRemoteNotification:application userInfo:remoteNotif];
            return YES;
        }

        return YES;
    }

您可以将逻辑从didReceiveRemoteNotification到一些通用函数并从两个地方调用该函数。仅当用户通过点击通知打开应用程序时,此功能才有效。如果用户通过点击应用程序图标打开应用程序,通知数据将不会到达应用程序。

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

应用程序首次启动时调用 didReceiveRemoteNotification 的相关文章

  • 在带有 Storyboard 的 XCode 4 中以模态方式推送视图时,出现“对开始/结束外观转换的不平衡调用”警告

    在网上进行了一些研究但没有成功后 我来这里向您询问有关我的警告的问题 实际上 我有一个带有导航控制器的视图 V1 我想在 V1 完成加载时推送模态视图 V2 所以我用performSegueWithIdentifier方法 我正在使用故事板
  • 如何解决 CoreData mogenerator 未找到问题

    我收到如下所示的错误 我不知道我错过了什么 我该如何解决这个问题 如下图所示 Users nischalhada Documents XcodePro mnepalnews revisited 2 0 CoreData mogenerato
  • 缺少常规选项卡

    我刚刚切换到使用 Xcode 5 我已经用一些需要更改的其他设置更改了我的应用程序的名称 但是当我执行此操作时 我注意到我的 常规 选项卡丢失了 有谁知道为什么会发生这种情况 只是快速更新如何在 Xcode 8 中执行此操作
  • 使用 NSError 检查错误的正确结构

    我正在编写各种例程 并尽力保持其整洁和重构 我正在创建的方法开始看起来与此代码类似 IBAction buttonPress id sender Create Document Shopping List with this documen
  • iOS 滚动视图允许滚动过去的内容

    我正在努力优化我的应用程序以适应横向和较小的屏幕 我主要使用滚动视图来实现这一点 在我的其中一个视图中 我有一个容器视图 当我在故事板中的设备之间切换时 这个容器视图看起来很完美 容器视图映射到具有滚动视图的视图控制器 该滚动视图的顶部 底
  • iphone NSDate 转换问题

    在我的 facebook 图表 Api 中 我正在获取这些数据 来自杰森 updated time 2011 05 17T14 52 16 0000 我正在使用此代码将其转换为有效的日期格式 NSDateFormatter df NSDat
  • 个人帐户开发者之间的 Apple 开发/分发证书

    我一直在到处寻找有关处理证书的正确答案 想象一下以下帐户 Joe拥有个人 Apple 帐户 但他根本不会编码 他只是发布了该应用程序并将其称为自己的 Bob还有一个个人 Apple 帐户 Bob 是一位编码专家 Joe 付费让他开发他的第一
  • 使用 Android Firebase 堆栈推送通知

    我开发了使用 Firebase 接收推送通知的 Android 应用程序 我的代码基于 Firebase Google 官方文档 https firebase google com docs cloud messaging android
  • 架构armv7的重复符号

    尝试在我现有的应用程序中使用 Layar SDK 时出现以下错误 我该如何解决这个问题 Ld Users pnawale Library Developer Xcode DerivedData hub afxxzaqisdfliwbzxbi
  • UICollectionView 拖放文件夹创建

    我正在使用 UICollectionView 创建 iOS 画廊应用程序 我希望用户能够拖放图像来重新排序图库并创建文件夹 类似于 iPhone 上的主屏幕 我发现了以下内容tutorial http nshint io blog 2015
  • 频繁绘制 CGPath 时的性能

    我正在开发一个将数据可视化为折线图的 iOS 应用程序 该图被绘制为CGPath在全屏自定义中UIView最多包含 320 个数据点 数据经常更新 图表需要相应地重新绘制 刷新率为 10 秒就很好了 到目前为止很容易 然而 我的方法似乎需要
  • 致命错误:在字典中发现“地理编码地标”类型的重复键。 (Mapbox 地理编码器)

    我引用 这通常意味着要么该类型违反了 Hashable 的要求 要么此类字典的成员在插入后发生了变化 我正在使用 Mapbox Geocoder 当发生此运行时错误时 我的 XCode 将我带到线程 1 0 swift runtime on
  • Google 地图 API -> OpenGLES 崩溃

    日志是从 Crashlytics 粘贴的 对于许多用户来说 崩溃经常发生 据我所知 它与设备 iOS 版本无关 我在我的代码中找不到任何错误 这似乎是纯粹的库问题 是 Google 地图 API 错误吗 我可以做些什么来修复它 或者我应该在
  • 指定访问组时出现 KeychainItemWrapper 错误

    相当长一段时间以来 我一直在使用 KeychainItemWrapper 的 ARC 版本成功读取和写入私有钥匙串项目 我现在正在努力将我的 iOS 应用程序转换为使用共享访问组 以便我的 2 个共享相同应用程序前缀的应用程序可以访问钥匙串
  • 通过 Instruments 使用 UIAutomation 操作 iphone 时,什么是 UIATargetHasGoneAWOLException。

    我正在使用 UIAutomation 通过 Xcode 中的 Instruments 在 iPhone 上执行某些操作 但遇到了一个奇怪的异常 UIATargetHasGoneAWOLException 有人知道这个异常是什么意思吗 我发现
  • 苹果企业程序分发问题[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 这个问题涉及到Apple iOS 开发者企业计划 http developer apple com programs ios enterprise 我
  • 如何反转 CGPath 的点顺序

    我想画一个圆圈 并用它打出字母 为此 我需要顺时针抚摸圆圈 逆时针抚摸字母 这一切都很好 但是当我使用 Core Text 获取字母路径时 我不知道如何从本质上反转该路径 不是镜像或旋转或任何东西 这很简单 我希望点笔画顺序是逆时针的 这实
  • 服务器到 Phonegap 推送:SignalR 与 Azure 通知中心

    好吧 我在继续开发 Phonegap 应用程序时陷入了两难境地 对于实时事件通知 我应该使用 Azure 通知中心还是 SignalR 据我了解 SignalR 通过使用 Web Sockets 非常适合实时 Web 应用程序 而通知中心可
  • Swift C 回调 - Swift 类指针的 takeUnretainedValue 或 takeRetainedValue

    我有一些UIView or UITableViewCell 里面我有 C 回调 例如 CCallback bridge self observer data gt Void in let mySelf Unmanaged
  • 分发内部业务 IOS 应用程序

    我遇到了 IOS 应用程序分发的一个令人困惑的部分 因此 我需要简单细分一下我的限制 即仅将我的应用程序分发给我的员工 同事或任何被视为 内部 的人 这是表明我不希望该应用程序出现在应用程序商店中的另一种方式 我的情况是我为几家公司开发 他

随机推荐

  • 在Qt中,QEvent意味着失去窗口焦点,重新获得窗口焦点? (设置透明度)

    当我的应用程序失去焦点时 我需要设置透明度 我还需要在重新获得焦点时重置透明度 通过鼠标单击或 alt tab 或其他方式 我知道如何设置透明度 所以这不是问题 设置窗口不透明度 0 75 问题是何时 我同意 K vin Renella 的
  • 从 SecureRandom 获取确定性值?

    出于基准测试的目的 我希望 SecureRandom 产生确定性的输出 这是否可以通过使用种子 或者算法的规范 来实现 import java security SecureRandom class TestSecureRandom pub
  • 在 Vite 中禁用预加载

    我正在将一个使用 Vue 2 和 Webpack 的大项目迁移到 Vue 3 和 Vite 到目前为止 一切看起来都很棒 但是当我们第一次尝试发布到生产环境时 我们注意到注入了许多模块预加载标签 并且其中的许多文件可能永远不会被使用 问题是
  • python lxml 与 py2exe

    我已经用 dom 生成了一个 XML 我想使用 lxml 来漂亮地打印 xml 这是我用于漂亮打印 xml 的代码 def prettify xml xml str import lxml etree as etree root etree
  • 使用 data.table 将 R 中的许多列乘以特定的其他列?

    我在 R 中有一个很大的 data table 其中有几个带有美元值的列 在另一列中 我有一个通货膨胀调整数字 我试图弄清楚如何用它乘以通货膨胀调整栏来更新我的每个货币栏 假设我有数据 DT lt data table id 1 1000
  • Playframework 2.1 找不到 javax.persistence 和 play.db

    我在使用 Play 框架 版本 2 1 创建实体的 Scala 教程中遇到问题 我正在尝试做 import java util import javax persistence import play db jpa 但是当我编译时 它告诉我
  • 更改 moreNavigationController 图标的颜色

    我已经成功地使用更改了导航栏色调 背景颜色和标签颜色this https stackoverflow com questions 438381 customizing the more menu on a tab bar 但是可以改变图标的
  • 检测 UITableView 部分标题何时捕捉到屏幕顶部

    我想检测何时UITableView节标题捕捉到屏幕顶部 然后修改标题的高度 但我不知道该怎么做 有人可以帮忙吗 我能够通过使用来完成检测哪个标题粘在表视图的顶部didEndDisplayingHeaderView and willDispl
  • 在jHtmlArea中添加表格

    有人知道使 jHtmlArea 支持表格内容并允许用户将表格 单元格 行添加到编辑器框的方法吗 它似乎不是该插件的标准功能 我希望我的用户能够在所见即所得编辑器中创建表 试试这个代码 只需将 editor 更改为您为文本区域设置的 id 即
  • 根据 OnKeyUp 中的文本框过滤列表框项目?

    我有一个包含大量项目的列表框 这些项目都是帐号 因此很难搜索 当用户在文本框中键入内容时 是否可以对项目进行 过滤 以便仅显示与迄今为止输入的内容相匹配的项目 e g List Box 2342 3434 2332 3224 然后用户在文本
  • R - ggplot2 直方图的阴影部分

    所以我有这个数据 dataset rbinom 1000 16 0 5 mean mean dataset sd sd dataset data subset subset dataset dataset gt mean 2 sd data
  • 错误:环境中未设置 XDG_RUNTIME_DIR。 Gtk-警告 **:无法打开显示:

    我曾经用 sublime 打开文本文件 对于只读文件 我曾经这样做 sudo sublime 但现在突然间sudo sublime命令给出以下错误 sublime 3931 Gtk WARNING cannot open display w
  • 无法调用类型缺少调用签名的表达式

    我有苹果和梨 两者都有isDecayed属性 interface Apple color string isDecayed boolean interface Pear weight number isDecayed boolean 这两种
  • 如何在闪亮的仪表板侧边栏中的 menuSubItem 下包含输入小部件?

    如何在闪亮应用程序的侧边栏中的 menuSubItem 下包含一个控件小部件 这是我的试验 library shiny library shinyWidgets library shinydashboard ui lt dashboardP
  • 使用 Ninject 将存储库注入自定义会员资格提供程序

    我正在尝试使用 MVC 3 中的 ninject 将存储库注入到自定义成员资格提供程序中 在 MembershipProvider 中我尝试了以下操作 Inject public ICustomerRepository customerRe
  • 如何更新外键

    我有 2 张桌子 第一个表的设计如下 Table 1 id Doc line sheet pk Autonumber DocNo text lineNo text Sheet No text 字段组合 DocNo lineNo Sheet
  • 如何禁用 Internet Explorer 的同源策略

    Chrome允许我们禁用同源策略 因此我们可以测试跨源请求 我想知道是否有可能在 IE 中做同样的事情 是的 您可以在 Internet 选项 中进行设置 转到 安全 选项卡 对于当前区域 单击 自定义级别 按钮 在下一个窗口中 向下滚动大
  • 更改底层依赖项后 SQL Server 视图显示过时/错误的数据

    我们有一个视图 称为 X 它是由另外 2 个视图 称为 Y 和 Z 调用的基本视图 今天我们对视图 X 进行了更改 之后视图 Y 和 Z 开始带回不正确的数据 当我们在 Management Studio 中运行时SELECT FROM Y
  • Javascript - 检索对象属性路径[重复]

    这个问题在这里已经有答案了 我有以下对象 var obj obj foo obj foo bar I want this 给定 路径 foo bar 作为字符串 我如何检索obj foo bar or obj foo bar 这是一个方法
  • 应用程序首次启动时调用 didReceiveRemoteNotification

    我已经实现了 didReceiveRemoteNotification 方法 它工作并显示一个视图控制器以及传递的通知数据 仅当应用程序已在前台或在后台运行时 此功能才有效 但是 当应用程序未运行并且用户单击通知时 应用程序会启动 但看起来