web-dev-qa-db-fra.com

Push Notification ON ou OFF Vérification dans iOS

Je veux vérifier "l'option de notification push" sur le périphérique iOS, à tout moment si l'application est en cours d'exécution (ou ON à partir du mode de reprise). J'utilise le code suivant pour vérifier si l'option est désactivée:

-(void)PushNotificationServiceChecking
{
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

    if (types == UIRemoteNotificationTypeNone)
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"ON", nil];
        alert.tag = 2;
        [alert show];
    }
}

Ensuite, j'utilise le code suivant pour accéder à l'onglet "Paramètres >> Centre de notification", afin que l'utilisateur puisse y accéder manuellement:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        if (buttonIndex == 0)
        {
            // this is the cancel button
        }
        else if (buttonIndex == 1)
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];
        }
    }

}

Mais, maintenant le problème auquel je suis confronté est, il n'apparaît qu'à la 1ère fois après le lancement de l'application. Ça marche comme je veux. Mais après cela, si je désactive "l'option de notification push" dans "paramètres", cela ne me donne aucun "message d'alerte".

21
Tulon

Si l'application s'est une fois enregistrée avec le registerForRemoteNotification, vous pouvez désactiver ou activer. Une fois que vous avez désactivé et que vous êtes sur le point de vous réenregistrer avec, cela activera le registerForRemoteNotification, sans Popup pour une alerte.

Note technique TN2265: Dépannage des notifications push

La première fois qu'une application compatible Push s'enregistre pour les notifications Push, iOS demande à l'utilisateur s'il souhaite recevoir des notifications pour cette application. Une fois que l'utilisateur a répondu à cette alerte, celle-ci ne s'affiche à nouveau que si l'appareil est restauré ou si l'application a été désinstallée pendant au moins une journée.

Si vous souhaitez simuler une première exécution de votre application, vous pouvez laisser l'application désinstallée pendant une journée. Vous pouvez atteindre ce dernier sans attendre réellement un jour en réglant l'horloge système d'un jour ou plus, en éteignant complètement l'appareil, puis en le rallumant.

Pour plus d'informations: INFO && Info 2

Modifier : Pour checking with alert enable -

utilisation

 if (types & UIRemoteNotificationTypeAlert){} 

au lieu de

if (types == UIRemoteNotificationTypeNone){}

Edit: Dernière mise à jour de la doc pour iOS 8 ou version ultérieure , vous pouvez vérifier par:

- (BOOL)isRegisteredForRemoteNotifications
21
Kumar KL

Dans iOS 8, vous pouvez désormais utiliser:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

Et pour vérifier comment les paramètres sont configurés, vous pouvez utiliser:

[[UIApplication sharedApplication] currentUserNotificationSettings];
41
thijsai

C'est du travail pour moi. J'espère que cette aide! :RÉ

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)


if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
    UIUserNotificationType type = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
    if (type == UIUserNotificationTypeNone){
        ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);
    }
}
else {
   UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
   if (types == UIRemoteNotificationTypeNone) {
       ALERT_WITH_TITLE(@"", kMessageNotificationTurnOnRequire);
   }
}
8
777Q
NSString *iOSversion = [[UIDevice currentDevice] systemVersion];
NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject];
float versionVal = [prefix floatValue];

if (versionVal >= 8)
{
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone)
    {
        NSLog(@" Push Notification ON");
    }
    else
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert_Push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil];
        alert_Push.tag = 2;
        [alert_Push show];

        NSLog(@" Push Notification OFF");
    }
}
else
{
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types != UIRemoteNotificationTypeNone)
    {
        NSLog(@" Push Notification ON");
    }
    else
    {
        NSString *msg = @"Please press ON to enable Push Notification";
        UIAlertView *alert_Push = [[UIAlertView alloc] initWithTitle:@"Push Notification Service Disable" message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Setting", nil];
        alert_Push.tag = 2;
        [alert_Push show];

        NSLog(@" Push Notification OFF");
    }
}
4
Maulik Salvi