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

时间:2016-09-27 08:15:37

标签: ios objective-c push-notification

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

代码:

@Html.RadioButtonFor(x => x.Account, Account.A1, new {disabled = "disabled"} )

1 个答案:

答案 0 :(得分:2)

在iOS 10中,您必须先在UNUserNotificationCenterDelegate文件

中设置AppDelegate.h
@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 );
}

    }

Apple在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);
       }

就是这样。

试试这个