热门标签 | 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. 当应用程序处于后台或处于非活动状态或已终止时,通知会进入并且用户可以点击它

推荐阅读
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社区 版权所有