user1324887
user1324887

Reputation: 672

IOS notification showing twice with default and my notification alert

I'm not an iOS programmer so bear with me. I'm able to get the push notifications but I get two alerts on my screens.

Issue 1: -One with the UIAlertView* alertWindow I am creating below - Second seems to be the default notification alert window. What am I doing wrong thats creating the second window ?

Issue 2: When the device is locked, I can see the notifications on screen but this is the default notification window. I don't see the notification with the details I'm getting in didReceiveRemoteNotification.

Also, I would appreciate If someone can explain the if else block in didFinishLaunchingWithOptions. I copied pasted it.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSString *AMAZON_SERVER = @"xxxxxx";

    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                UIUserNotificationTypeBadge |
                                                UIUserNotificationTypeSound);
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                         categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
        configuration.applicationId = APPLICATION_ID;
        configuration.clientKey = CLIENT_KEY;
        configuration.server = AMAZON_SERVER;
    }]];

    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

    [Fabric with:@[[Digits class]]];

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];

    if([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]!=TRUE)
    {
        [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"FirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    //storedevice Type in standardUserDefaults
    [self setDeviceType];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [PFPush handlePush:userInfo];
    if ([userInfo objectForKey:@"aps"]) {
        NSMutableDictionary * apsData = [userInfo objectForKey:@"aps"];
        NSString* alert = [apsData objectForKey:@"alert"];
    ...
    ...

        UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
                                                    message: message
                                                   delegate: self
                                          cancelButtonTitle: @"OK"
                                          otherButtonTitles: nil];

        [alertWindow show];
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    } else {
        [PFPush handlePush:userInfo];
    }
}

Upvotes: 0

Views: 389

Answers (2)

HariKrishnan.P
HariKrishnan.P

Reputation: 1204

 First uninstall the app and try it. It will work fine same problem i am facing.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
        configuration.applicationId = APPLICATION_ID;
        configuration.clientKey = CLIENT_KEY;
        configuration.server = AMAZON_SERVER;
    }]];

    [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];

    [Fabric with:@[[Digits class]]];



    if([[NSUserDefaults standardUserDefaults] boolForKey:@"FirstLaunch"]!=TRUE)
    {
        [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"FirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    //storedevice Type in standardUserDefaults
    [self setDeviceType];
       #here i am edited

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)])
   {


    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

}
else
{

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];


}



    return YES;
}
// remove the below methods

    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Store the deviceToken in the current installation and save it to Parse.
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //[PFPush handlePush:userInfo];
    if ([userInfo objectForKey:@"aps"]) {
        NSMutableDictionary * apsData = [userInfo objectForKey:@"aps"];
        NSString* alert = [apsData objectForKey:@"alert"];
    ...
    ...

        UIAlertView* alertWindow = [[UIAlertView alloc] initWithTitle: alertHeader
                                                    message: message
                                                   delegate: self
                                          cancelButtonTitle: @"OK"
                                          otherButtonTitles: nil];

        [alertWindow show];
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    } else {
        [PFPush handlePush:userInfo];
    }
}

Upvotes: 1

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

Your if statement says that if your [UIApplication sharedApplication] instance has implemented registerUserNotificationSettings: method, then it will execute if block, otherwise it will execute else block.

Upvotes: 0

Related Questions