当app在后台时,当我点击通知项目(在通知栏中)时应用程序调用了哪个方法?

时间:2017-01-12 02:38:39

标签: java ios notifications

我在互联网上搜索发现:在iOS7之后,即使应用程序被杀,如果用户点击与该应用程序关联的通知,该应用程序也会调用此方法:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler 

我正在使用Adhoc配置文件,应用程序处于后台并且未被杀死。

我的代码如下:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {

// JPush
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo];  // handle the notification 
completionHandler(UIBackgroundFetchResultNewData);

NSLog(@"%@--userinfo", userInfo);
NSLog(@"%ld--applicationState", (long)[UIApplication sharedApplication].applicationState);

当我测试时,设备日志中没有任何内容。

修改-1

我在application:didFinishLaunchingWithOptions中注册APN的代码: 我的iphone操作系统版本为10.2

// 7.push  regist
if (IOS10) {
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"succeeded!");
        }else {
            NSLog(@"error:%@", error);
        }
    }];
} else if (IOS8_10){//iOS8-iOS10
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
} else {//iOS8以下
    [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}

修改-2

请参阅Chandan的评论,如果将content-available = 1添加到aps字典,我们可以唤醒应用,但如何将值添加到aps使用{{ 1}}:

enter image description here

代码如下:

java

3 个答案:

答案 0 :(得分:3)

仅当您将Application:didReceiveRemoteNotification:键值content-available添加到通知有效内容中时,

1才会在后台调用。

您可以在应用程序处于前台时检查content-available

NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];

供参考: - Receiving Push Notifications while in background

答案 1 :(得分:0)

看起来您正在尝试接收远程推送通知。你还记得注册远程推送通知吗?此外,您还需要启用推送通知功能。

基本上,有三个步骤。

  1. 为您的应用启用推送通知功能。
  2. 在应用的发布时间代码中注册APN。
  3. 实施对处理传入远程通知的支持。
  4. 您展示的代码仅适用于第3步。

    请参阅Apple reference on push notifications set up

答案 2 :(得分:0)

尝试这些代码行,我猜他们会解决您的问题

这是注册远程通知并处理它们的代码 UNUserNotificationCenterDelegate添加此委托

if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound |    UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

        NSLog( @"registerForPushWithOptions:" );
    }
    else
    {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
         {
             if( !error )
             {
                 [[UIApplication sharedApplication] registerForRemoteNotifications];  // required to get the app to do anything at all about push notifications
                 NSLog( @"Push registration success." );
             }
             else
             {
                 NSLog( @"Push registration FAILED" );
                 NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
                 NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );  
             }  
         }];  
    }

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
        UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (grantedSettings.types == UIUserNotificationTypeNone) {
            NSLog(@"No permiossion granted");
        }
        else {
            NSLog(@"Permission Granted");
        }
    }

    -(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void
            (^)(UIBackgroundFetchResult))completionHandler

            {

                // iOS 10 will handle notifications through other methods

                NSLog(@"Notification payload is %@", userInfo);

                if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )

                {

                    NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );

                    // set a member variable to tell the new delegate that this is background

                    return;

                }

                  // custom code to handle notification content

                if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )

                {

                    NSLog( @"INACTIVE" );

                    completionHandler( UIBackgroundFetchResultNewData );

                }

                else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )

                {

                    NSLog( @"BACKGROUND" );

                    completionHandler( UIBackgroundFetchResultNewData );

                }

                else

                {

                    NSLog( @"FOREGROUND" );

                    completionHandler( UIBackgroundFetchResultNewData );

                }

            }





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

        {  

            [self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {  

            }];  

        }



        - (void)userNotificationCenter:(UNUserNotificationCenter *)center

               willPresentNotification:(UNNotification *)notification

                 withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler

        {

            NSLog( @"Handle push from foreground" );

            // custom code to handle push while app is in the foreground

            NSLog(@"%@", notification.request.content.userInfo);

        }



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

            NSLog( @"Handle push from background or closed" );

            // if you set a member variable in didReceiveRemoteNotification, you  will know if this is from closed or background

            NSLog(@"%@", response.notification.request.content.userInfo);
        }