I'm trying to enable push notification in my app but it's not working. How ever it was working in my last app. When i integrated it in that app. but now i again test that app it's not working in that app also. By mean of not working is that it's not showing the confirmation or enabling pop view for push notification.i am using 4.6 xcode version and using this line of code for enabling the push notification
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
is there is some issue in new xcode or sdk or there is some change in the code now ?
As far as I know only once you get the notification to enable the PushNotification . i.e the notification which is showing the message "Would you like to enable the notification ........." . Once you click OK on one device you didn't get that same alert on same device on next run . So try to check it on other device or debug the code using the Developer Profile .
In the delegate methods log the error in did fail method and verify
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err);
}
is the provisioning profile matter ? i mean i am using the wild card
provisioning profile for testing the push notification
Yes, the provisioning profile matters. It must contain a push entitlement for the environment to which you intend to send push notifications (either sandbox or production).
From the Local and Push Notification Programming Guide :
The Team Admin or Team Agent must next create the provisioning profile
(Development or Distribution) used in the server side of
remote-notification development. The provisioning profile is a
collection of assets that associates developers of an application and
their devices with an authorized development team and enables those
devices to be used for testing. The profile contains certificates,
device identifiers, the application’s bundle ID, and all entitlements,
including <aps-environment>. All team members must install the
provisioning profile on the devices on which they will run and test
application code.
And you can't use a wild card provisioning profile. You must use a provisioning profile with a non-wildcard Application ID.
From the Local and Push Notification Programming Guide :
The next page displays your valid application IDs. An application ID
consists of an application’s bundle ID prefixed with a ten-character
code generated by Apple. The team admin must enter the bundle ID. For
a certificate, it must incorporate a specific bundle ID; you cannot
use a “wildcard” application ID.
Related
I have a iPhone app that uses Push Notifications. My understanding of how this works is I need the "Device Token" of each iPhone before I can send a notification.
Using the test iPhones I have, I can obtain the Device Token from the xcode interface and store them in a data table which the Push Notification PHP script uses to send the notifications.
How do I send the Push Notifications to iPhones that install the app of which I do not know the Device Token ID.
I think my question is; do I need the Device Tokens before I can sent a notification to a iPhone.
If I do require the Device Token, how do I obtain it from iPhones using my app.
You can't receive any Push Notification with the Simulator.
You don't directly send a notification. You said to Apple's servers that there is a notification for this Device Token. Then iPhones themselves will ask automatically to receive push notifications is some are available.
To answer to your question : Yes you do. Without the Device Token you can't register your notification to Apple's server. Then you have to implement this register the device to get the DeviceToken:
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
Here is you should look up == Push Notification Programming Guide
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(#"Error in registration. Error: %#", err);
}
See the code from apple Local and Push Notification Programming Guide, first you have to call registerForRemoteNotificationTypes in your app, and then in two delegates, you can get dev token or not, this token is from APNS, so when you get it, you will send it to your service to store it. And you can't get a device token from simulator.
Your push notification service will have to use the device token to send notification to your app, so you will definitely need it. In additional, you will have to get SSL certificates from apple dev center, that requires an valid paid developer ID of iOS.
For more information, please check Push Notification Programming Guide, and it's a great help.
Furthermore, I tried this PHP library to test push notification service, and it's good, you can just find its unit test, and paste the device token there, and run it, you will get message from it.
sly/notification-pusher
The first time I run my app, I get asked if I want to allow push notifications, so I select yes. Now, when I go to settings and turn off all notifications for my app, I still get a device token from - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken;. Am I, as the dev, responsible to track if my app has any enabled? Or is this a bug?
The device token is generally stored on a server someplace. If the user suddenly decides they want push notifications and turns it back on, their device token has already been generated and stored on your server.
If the device token was not generated, then the user would have to make sure the app has been terminated and then re-launched in order for the device token to get generated.
You can determine if the user has turned off push notifications with the code below
UIRemoteNotificationType remoteNotificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (remoteNotificationType == UIRemoteNotificationTypeNone) {
... Do not send push
}
I have tried out the example found in the link http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 , for my iPhone push notification message. I could successfully run all steps except the steps for registering the app for push notification for the first time.
I have followed different variations of the code:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
But none worked for me. I am testing it in my iPhone 4 and my Xode version is 4.2 , OS is Lion.
Please advice.
You can try the following:
Make sure your Xcode project is configured to use the explicit bundle ID from the provisioning portal
Make sure you're not building the target with wildcard or team provisioning profile, check this on the project AND target settings
If you created your provisioning profile before configuring the app ID for push, regenerate the provisioning profile
Also look at the error object in the UIApplicationDelegate method
application:didFailToRegisterForRemoteNotificationsWithError:
EDIT 1
Open your provisioning profile in a text editor and look for the string
<key>aps-environment</key>
If your profile does not contain this, it is not correctly set up for push.
Here is my workin code sample from application delegate (AppDelegate.m)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
To check if registration went OK, implement these three methods
– application:didReceiveRemoteNotification:
– application:didRegisterForRemoteNotificationsWithDeviceToken:
– application:didFailToRegisterForRemoteNotificationsWithError:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Or you can go to Settings to see if there is permissions settings available
like this:
If this problem appears in production (with your application on the appStore), it's very likely because you created/enabled the APNS production mode of your application AFTER having created the distribution profile.
In this case, no other option but to submit again with a correct certificate...
Rule of thumb: ALWAYS enable/create production certificate for your application BEFORE creating the distribution profile.
You can use the method described by 'nduplessis' (see below) to manually check if you application will run notification once approved.
iOS 14 ?
For those who faced with inability to get token, and double checked all the things mentioned above, and developed on iOS 14, and if you have non-english app title, go to Project - Target - Product Name and replace name by $(TARGET_NAME).
I spent entire week on this bug.
Once again: if your app receives token on iOS12, but can not on iOS14, get rid of non-latin chars in names. They say, including entitlements, but as for me, I've managed to make it work and with cyrillic entitlements. To change Product name was enough.
Have you ever seen the push notification dialog in your app? If you dismiss the dialog it takes 24 hours to reshow. Check this by setting the time on your iPhone ahead by 2 days and see if it shows.
Also have you implemented the delegate methods:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
Try putting an NSLog(#"") in the didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError and see it is is getting in there?
Also you require an App Id that is setup for Push notification and you need to have generated the Push notification certificates to send the messages.
You can only register it with a provisioning profile having an app id with push notification services enabled.please Check those steps also.This a good tutorial for starting push notification http://mobiforge.com/developing/story/programming-apple-push-notification-services
Go to Capabilities -> Push Notifications. If there are errors then try out this solution!
If Xcode version was changed from 7 to 8 then try out this solution!
Xcode 7 copies the aps-environment entitlement from your provisioning profile. Turning on push notifications on Xcode 8 adds the entitlement both to your provisioning profile and the entitlements file.
Try adding the aps-environment entitlement to your app's entitlements file, or switch the notification capability off and on again.
I am running Push Notification on two test apps. The first test app I got working over a 2 day period. The 2nd test app I tried to implement Push Notification tonight and have been banging my head over it. I am able to swap the certificates and the 1st app receives notifications, but when I use the .pem for the 2nd app, nothing happens.
I have gone through the entire process twice tonight, trying to get the Push to work for this 2nd app. My only conclusion, given that I can swap the .pems and it will work for the 1st app, is that something is wrong with the 2nd .pem or maybe there is a waiting period before the push starts working, and I just didn't realize it during my first setup?
Edit: The problem was that I was using
-(BOOL)application: didFinishLaunchingWithOptions:
in my delegate and not using
-(void)applicationDidFinishLaunching:
I was doing...
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Register for Push Notifications
UIRemoteNotificationType notifyTypes = (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge);
}
When I should have been doing
-(void)applicationDidFinishLaunching:(UIApplication *)application {
//NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
// Register for Push Notifications
UIRemoteNotificationType notifyTypes = (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound |UIRemoteNotificationTypeBadge);
}
There is a waiting period before Push starts working, though in my experience said period is generally quite short. Make sure that the little dot is green in your App ID page of the provisioning portal - and make sure that it is for the correct App ID and environment (for example, if you're running in debug, make sure that it is configured for development).
Apart from that, it could be an issue with your .pem or provisioning profile as well.
From the Apple Push Reference guide:
You have to modify the profile in some minor way (for example, toggle an option) for the portal to generate a new provisioning profile. If the profile isn't so “dirtied,” you're given the original profile without the push entitlements.
Do you have any errors coming up when you attempt to push?
I am new to this topic and require some guidance in implementing Apple Push Notification in my application. I have created my appID and also configured Apple Push Notification for the same. I have downloaded the provisioning profile and installed the app on the iphone. I have also written the following code provided by Apple documentation
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
const void *devTokenBytes = [devToken bytes];
NSLog(#"devToken=%#",devTokenBytes);
//self.registered = YES;
//[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
NSLog(#"Error in registration. Error: %#", err);
}
I want to know what I have to write on the server side. When I run the code it says that the device is not registered. How can I register my application for the push notification.
Can anyone help me with this...
Any code will be very helpful...
Thanx in advance...
You need to tell your server about the device token returned by Apple when you register for notifications from the device, so that the server can present the same token and app id when it tells the apple server that there's a new notification. Have you done that? I believe the device token could change each time you register, so you'll need to keep track of that on your server (and tell the server each time).
You've shown the callbacks involved in device registration, but have you actually called the registration method itself?
You also got to listen to didReceiveRemoteNotification in case you want to know when notifications arrive also when the app is in foreground. You might also want to clear the badge number set on the app icon when the user has read the notification it was sent.