2 votes

Comment gérer les notifications à distance d'iOS lorsque l'application est en arrière-plan ?

Je travaille sur la fonctionnalité de notification push d'iOS par le biais de la notification push d'apple, pour l'instant je reçois une notification appropriée lorsque mon application est en arrière-plan ou au premier plan, mais je veux gérer la notification à distance lorsque mon application est en arrière-plan fondamentalement, lorsque mon application est en arrière-plan, elle montre simplement le message d'alerte de la charge utile.

code :

 - (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"]);
}

2voto

Himanth Points 1444

Dans iOS 10, vous devez d'abord définir UNUserNotificationCenterDelegate sur AppDelegate.h fichier

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate,UNUserNotificationCenterDelegate>

Après cela, dans AppDelegate.m écrire le code comme ceci

- (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;
}

Maintenant, mettez en œuvre cette méthode pour la version iOS10 ci-dessous

    - (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 introduit ces deux méthodes dans iOS10 pour recevoir des notifications push.

Écrivez également ces méthodes

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

C'est tout.

Essayez ceci

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X