热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

单击后为什么远程通知操作无效?-Whyremotenotificationactionisdoingnothingafterclicking?

IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff

I have configured an action for a remote notification when it arrives to my iOs app. I want two different actions whether one key passed in the payload. The code seems to be executed, but the app doesn't open, nor the safari url. Here is my AppDelegate.m:

我已经为远程通知配置了一个动作,当它到达我的iOs应用程序时。我想要两个不同的操作,无论一个密钥是否在有效负载中传代码似乎已执行,但应用程序无法打开,也不会打开safari网址。这是我的AppDelegate.m:

NSString *const NotificatiOnCategoryOpenView= @"openView";
NSString *const NotificatiOnActionOpenView= @"View";

- (void)registerForNotification {
    UIApplication *application = [UIApplication sharedApplication];

    // iOs 8 or greater:

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        UIMutableUserNotificationAction *open;
        open = [[UIMutableUserNotificationAction alloc] init];
        [open setActivationMode:UIUserNotificationActivationModeBackground];
        [open setTitle:NSLocalizedString(@"View", nil)];
        [open setIdentifier:NotificationActionOpenView];
        [open setDestructive:NO];
        [open setAuthenticationRequired:NO];

        UIMutableUserNotificationCategory *actionCategory;
        actiOnCategory= [[UIMutableUserNotificationCategory alloc] init];
        [actionCategory setIdentifier:NotificationCategoryOpenView];
        [actionCategory setActions:@[open] 
                        forContext:UIUserNotificationActionContextDefault];

        NSSet *categories = [NSSet setWithObject:actionCategory];
        UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                        UIUserNotificationTypeSound|
                                        UIUserNotificationTypeBadge);

        UIUserNotificationSettings *settings;
        settings = [UIUserNotificationSettings settingsForTypes:types
                                                     categories:categories];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else if ([application respondsToSelector:@selector(registerForRemoteNotificationTypes:)]) { 
        // iOs 7 or lesser:

        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        [application registerForRemoteNotificationTypes:myTypes];
    }

}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {

    if ([identifier isEqualToString:NotificationActionOpenView]) {

        NSDictionary *aps = userInfo[@"aps"];

        if ([[aps allKeys] containsObject:@"viewToOpen"]){
            NSString *webString = [[NSString alloc] initWithFormat:@"%@", aps[@"viewToOpen"]];
            NSURL *webURL = [[NSURL alloc] initWithString:webString];

// This line doesn't work:
            [[UIApplication sharedApplication] openURL:webURL];

        } else {
// These two lines doesn't work:
            UINavigationController *navigatiOnController= (UINavigationController *)self.window.rootViewController;
            [navigationController popToRootViewControllerAnimated:NO];
        }
    }

    completionHandler();

}

Thanks!

谢谢!

2 个解决方案

#1


0  

Implement

实行

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler 

And

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

Methods. Notification activation mode is set to UIUserNotificationActivationModeBackground,which Activate the app and put it in the background. If the app is already in the foreground, it remains in the foreground.So if you want to application in foreground/launch then use UIUserNotificationActivationModeForeground

方法。通知激活模式设置为UIUserNotificationActivationModeBackground,它激活应用程序并将其置于后台。如果应用程序已经在前台,它仍然在前台。所以如果你想在前台/启动中应用,那么使用UIUserNotificationActivationModeForeground

    [open setActivationMode:UIUserNotificationActivationModeBackground];

Use this activation option:

使用此激活选项:

    [open setActivationMode: UIUserNotificationActivationModeForeground];

Check weather webUrl is proper and working as expected in normal flow.

检查天气webUrl是否正常并在正常流程中按预期工作。

For Xcode 8, u will have to migrate push notification using UserNotification framework.

对于Xcode 8,您必须使用UserNotification框架迁移推送通知。

#2


1  

Your code should be in this method

您的代码应该使用此方法

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

    if(userInfo) {
        UIApplicationState state = [[UIApplication sharedApplication] applicationState];
        NSUInteger notificatiOnType= [[userInfo valueForKey:API_NOTIFICATION_TYPE] integerValue];
        if (state == UIApplicationStateActive) {

            // Write code which should work when notification comes and app is active
            }
            else {
                // Write code which should work when notification comes and app is in background or inactive or terminated
            }
        }
    }
}

This method will get called only

此方法仅被调用

  1. when app is ACTIVE and notification comes
  2. 当应用程序处于活动状态且通知到来
  3. when app is in background or inactive or terminated and notifications comes and user taps on it
  4. 当应用程序处于后台或处于非活动状态或已终止时,通知会进入并且用户可以点击它

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 使用Python在SAE上开发新浪微博应用的初步探索
    最近重新审视了新浪云平台(SAE)提供的服务,发现其已支持Python开发。本文将详细介绍如何利用Django框架构建一个简单的新浪微博应用,并分享开发过程中的关键步骤。 ... [详细]
  • 本文介绍了两种方法,用于检测 Android 设备是否开启了开发者模式。第一种方法通过检查 USB 调试模式的状态,第二种方法则直接判断开发者选项是否启用。这两种方法均提供了代码示例和详细解释。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 使用原生 AJAX 处理文件下载与 IE 浏览器图片显示问题
    本文探讨了如何利用原生 AJAX 实现文件的 POST 或 GET 请求下载,并监控下载状态。同时,针对 IE 浏览器及部分移动设备浏览器因路径问题导致图片无法正常显示的情况,提供了有效的解决方案。 ... [详细]
  • 本文提供了一套详细的步骤,指导用户如何通过科学上网方法注册一个美国地区的Apple ID,包括设置地区、语言及完成注册的具体操作。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文深入探讨了Linux系统中网卡绑定(bonding)的七种工作模式。网卡绑定技术通过将多个物理网卡组合成一个逻辑网卡,实现网络冗余、带宽聚合和负载均衡,在生产环境中广泛应用。文章详细介绍了每种模式的特点、适用场景及配置方法。 ... [详细]
  • MongoDB集群配置:副本集与分片详解
    本文详细介绍了如何在MongoDB中配置副本集(Replica Sets)和分片(Sharding),并提供了具体的步骤和命令,帮助读者理解并实现高可用性和水平扩展的MongoDB集群。 ... [详细]
  • 本文详细介绍了如何在 Spring Boot 应用中通过 @PropertySource 注解读取非默认配置文件,包括配置文件的创建、映射类的设计以及确保 Spring 容器能够正确加载这些配置的方法。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 我在项目中发现设置了 GCC_NO_COMMON_BLOCKS = NO 的配置项,位于 Apple LLVM 编译器 3.1 的代码生成设置中。 ... [详细]
author-avatar
榴莲味蛋筒
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有