Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone registerForRemoteNotificationTypes does not generate an error but does not fire delegate that gives device token

I am developing an iPhone app that needs push notification. I followed the instructions for creating the certifications and modifying the app ID. I am not totally sure I did this correctly, but I did follow the directions. Any idea how I can check to see if this is OK?

When I ran in the emulator I did get an error message saying that the emulator did not support push notifications. This was somewhat expected.

BTW: I have seem this question out there a few times. It always seems to be with a jail-broken phone. My phone is NOT jail-broken.

But when I debug on the iPhone the didRegisterForRemoteNotificationsWithDeviceToken method is never fired off. I will really appreciate some help. My code follows.

-(void)applicationDidFinishLaunching:(UIApplication *)application 
{    
   rootController.delegate = self;
   [window addSubview:rootController.view];
   [window makeKeyAndVisible];

   [[UIApplication sharedApplication] 
   registerForRemoteNotificationTypes:
   (UIRemoteNotificationTypeAlert | 
     UIRemoteNotificationTypeBadge | 
     UIRemoteNotificationTypeSound)];
}

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

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err 
{ 
    NSString *str = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(str);    
}
like image 409
Rick Leinecker Avatar asked Jul 11 '10 20:07

Rick Leinecker


People also ask

How do I delegate an app-specific device token?

The system then asynchronously calls one of two following app delegate methods, depending on success or failure: On successful issuance of an app-specific device token, the system calls the application:didRegisterForRemoteNotificationsWithDeviceToken: method. Implement this method to receive the token and forward it to your provider.

How do I handle remote notifications on an iOS device?

On an iOS device, if a user force-quits your app using the app multitasking UI, the app does not receive remote notifications until the user relaunches it. For an app to handle remote notifications, it must have the proper entitlements to talk to APNs.

How do I get the device token for remote notifications?

If remote notifications become available later, the app object notifies you by calling your delegate’s application:didRegisterForRemoteNotificationsWithDeviceToken: method. In macOS, you obtain a device token for your app by calling the registerForRemoteNotificationTypes: method of the NSApplication object.

What happens if an error occurs during remote notification registration?

If an error occurs during registration, the app temporarily disables any features related to remote notifications. Those features are reenabled when a valid device token is received. // Configure the user interactions first. // Register for remote notifications. // Handle remote notification registration.


4 Answers

Usually if didRegisterForRemoteNotificationsWithDeviceToken is never called, it is because you are using a provisioning profile that is not APS-entitled. If you app is a production app, it must be signed with a distribution profile for pushes to work. If it is a development app, you must sign it with a development profile.

EDIT: It is also worthy to note that the provisioning profile must use an explicit bundle id. Pushes sent to anything signed with a wildcard profile will not be delivered.

like image 106
Patrick Goley Avatar answered Oct 23 '22 15:10

Patrick Goley


I had the same issue.

For me, it was the notification setting for the App had everything turned off.

If in the iOS settings app notifications are disabled, that method will not get called.

Once I turned on a notification type it started to work.

Settings App->Notification Settings->[My App]

like image 31
Eric Avatar answered Oct 23 '22 14:10

Eric


Check out TN2265, which has hints for debugging push notification problems. I had exactly this problem today and it turned out that WiFi was turned off on iPod Touch. Since you said you were working from an iPhone. that's probably not it, but it's worth checking out the tech note for additional possibilities.

like image 2
Scott K. Avatar answered Oct 23 '22 13:10

Scott K.


Check your firewall settings.

From Scott K's Answer:

If your iOS device is capable of using the cellular data network, check that it has an active cellular data plan. Turn off Wi-Fi in Settings and see if you can still browse the web with Safari, for example. On the other hand, if the push service is using Wi-Fi, any firewalls between your device or computer and the Internet must allow TCP traffic to and from port 5223.

like image 1
Tony Ashworth Avatar answered Oct 23 '22 14:10

Tony Ashworth