Ios7.1: push notification icon refresh issue

I have a setup Push Notificationin one of my current projects. I followed all the instructions necessary for push notification. which works fine in [tag: ios7], but in 7.1I got a problem updating the icon when my application is in the background.

My code: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

}


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}


- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *str = [NSString
                     stringWithFormat:@"%@",deviceToken];
    NSLog(@"%@",str);

    self.deviceToken = [NSString stringWithFormat:@"%@",str];
    NSLog(@"dev --- %@",self.deviceToken);
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.deviceToken = [self.deviceToken stringByReplacingOccurrencesOfString:@">" withString:@""];
    NSLog(@"dev --- %@",self.deviceToken);
}

And to get an answer

-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{

    UIApplicationState state = [application applicationState];

    // If your app is running
    if (state == UIApplicationStateActive)
    {

        //You need to customize your alert by yourself for this situation. For ex,
        NSString *cancelTitle = @"ok";
      //  NSString *showTitle = @"Get Photos";
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:cancelTitle
                                                  otherButtonTitles:nil];
        [alertView show];


    }
    // If your app was in in active state
    else if (state == UIApplicationStateInactive)
    {

    }

     [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

}

First of all, when my application will return, so that it didReceiveRemoteNotificationdoesn’t work, I did a search and put: -

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

And set the background mode, for example: -

enter image description here

, xcode, Push Notification (iOS7.1). , . , . , iOS7, .

, . - , , , .

+4
2

Try:

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; // try 0 or -1
[UIApplication sharedApplication].applicationIconBadgeNumber = badge + [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];
+1

, :

NSDictionary *apsData = [userInfo objectForKey:@"aps"];
NSInteger badgeNumber = (NSInteger)[[apsData  objectForKey: @"badge"] intValue];

[UIApplication sharedApplication].applicationIconBadgeNumber = 
            [UIApplication sharedApplication].applicationIconBadgeNumber + badgeNumber;

PHP apple, :

$playload = array('aps'=>array(
                         'message'=> 'some message',
                         'badge'=> (int)$badge 
                 )
);
0

Source: https://habr.com/ru/post/1533745/


All Articles