0

Hi I am stuck with this issue for over a week now. I followed this tutorial here to add Push Notification in my app. On initial run of my app the "App would like to send you push notification" is not appearing. But when I go to the Apps Notification it is already registered for notification for sounds and banners only and the Badge App Icon is not ON. But when I Login in my app the Badge App Icon Notification Type is no longer there. (sorry cant post image)

And when I check my logs I have this run time error: Attempting to badge the application icon but haven't received permission from the user to badge the application

Here's my code in my AppDelegate.m

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
                                                                                        UIUserNotificationTypeBadge |
                                                                                        UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     UIRemoteNotificationTypeAlert |
     UIRemoteNotificationTypeBadge |
     UIRemoteNotificationTypeSound];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *hexToken= [[[[deviceToken description]
    stringByReplacingOccurrencesOfString: @"<" withString: @""]
    stringByReplacingOccurrencesOfString: @">" withString: @""]
    stringByReplacingOccurrencesOfString: @" " withString: @""];

    [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

Any help will be appreciated. Thanks.

  • Refer this link.. http://stackoverflow.com/questions/25973364/attempting-to-badge-the-application-icon-but-havent-received-permission-from-th – Nilesh Patel Apr 17 '15 at 04:20
  • Remove `[[UIApplication sharedApplication] registerForRemoteNotifications];` line in if block, and add the method as Nilesh is suggesting in his answer, and everything will work. And, no need for `#ifdef` statements. – Fahri Azimov Apr 17 '15 at 06:13
  • Parse have a good push notification service. The tutorials are also clear and work well: https://parse.com/tutorials/ios-push-notifications – sam_smith Apr 17 '15 at 06:50

1 Answers1

1

I just tried using below way & it worked..

#ifdef __IPHONE_8_0
  //Right, that is the point
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
    //register to receive notifications
    UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif

Add below method for iOS 8.0

#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //register to receive notifications
    [application registerForRemoteNotifications];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
    //handle the actions
    if ([identifier isEqualToString:@"declineAction"]){
    }
    else if ([identifier isEqualToString:@"answerAction"]){
    }
}
#endif
Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
  • i still get the same result..i think my problem is the pop up asking for permission for notification is not displaying..i tried setting the date 1 week ahead then turning off my device, but i still get the same result. any idea how i can display the pop up..? – Arianne Lee Apr 17 '15 at 04:34
  • Use another provisional profile configured with push notification & you will get the alert for notification for sure.. if everything will be fine.. – Nilesh Patel Apr 17 '15 at 04:36
  • i created new app id, new provisioning profile and i was able to show the pop up..but when i logged in in my app, same behavior I still get the runtime error: "Attempting to badge the application icon but haven't received permission from the user to badge the application" and the Badge App icon Notification Type is missing in the notification types.. – Arianne Lee Apr 17 '15 at 07:21
  • Please refer this link.. http://stackoverflow.com/questions/24028172/debug-output-mentions-i-should-ask-for-application-badge-permission – Nilesh Patel Apr 17 '15 at 09:44