如何在应用程序处于后台时处理 iOS 远程通知 [英] How to handle iOS remote notification while app is in background

查看:94
本文介绍了如何在应用程序处于后台时处理 iOS 远程通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on iOS push notification functionality through apple push notification,right now i am getting proper notification while my app is in background or foreground,but i want to handle remote notification when my app is in background basically when my app is in background it is simply showing alert message from payload.Actually i just want to customize my remote notification.

代码:

 - (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
//        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
return YES;
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", error);
}
- (void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken {
NSLog(@"Did Register for Remote Notifications with Device Token (%@)", deviceToken);
}
-(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (UIBackgroundFetchResult))completionHandler
{
NSDictionary * aps=[userInfo valueForKey"aps"];
NSLog(@"did recevie %@",aps);
NSLog(@"userinfo details %@",[aps valueForKey"alert"]);
}

推荐答案

在 iOS 10 中,首先你必须在 AppDelegate.h 文件中设置 UNUserNotificationCenterDelegate

In iOS 10 first you have to set UNUserNotificationCenterDelegate in AppDelegate.h file

@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以下版本实现此方法

Now implement this method for below iOS10 version

    - (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 中引入了这两种接收推送通知的方法.

Apple introduces these two methods in iOS10 to receive push notifications.

也要写这些方法

    // 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);
       }

就是这样.

试试这个

这篇关于如何在应用程序处于后台时处理 iOS 远程通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆