iPhone Push Notifications - iphone

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

Related

Test apple push notifications to multiple devices

I need to check apple push notifications to multiple devices. I am using JavaPNS and the push is working fine. But I have only one device token with me. How can I be sure that in real time (when there will be millions of device tokens - devices) , the push will work fine.
I already saw this:
load testing apple push notification server application
but am not sure that I understand it completely or for that matter is it the correct approach.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
self.mDeviceToken = deviceToken;
//Removing the brackets from the device token
NSString *tokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]];
NSLog(#"Push Notification tokenstring is %#",tokenString);
}
when you get the token you should send this token to your database using webservice. in that way you will get all the tokens who installed your app.

push notification is not working

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.

Push Notifications using Urban Airship correctly

I tried using Urban Airship for sending push notifications to the devices.
I register the device to push using:
Code:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
I created an app on Urban Airship, uploaded the certificate key and tried sending a broadcast with no success.
Only when I manually added the device token to Urban Airship, I successfully sent and got a broadcast.
My question is - can I use Urban Airship to send Push notifications without registering device tokens and how? If not, how do I register the device tokens automatically from the app (and not using the curl code)?
Is there any other push provider which doesn't require more than Apple's basic registerForRemoteNotificationTypes: method?
Thanks!
No, Urban Airhip cannot send push notifications if the device does not register on the network, think about that logic. registerForRemoteNotificationTypes: simply initiates the registration process and assumes you have done the rest of the leg work to make that magic happen.
You also need the rest of the delegates to properly register and receive Push Notifications on your device.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken;
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
In addition to these UIApplication delegates, you need to initiate the Airship classes by doing the following in your didFinishLaunchingWithOptions: delegate:
[Airship takeOff: kApplicationKey identifiedBy: kApplicationSecret];

why I can receive the old app's push notification when the iphone app reinstall?

uninstall the app first, then reinstall, I can receive the push notification normally.
here is my analysis:
client uninstalls app, but my provider server has the devicetoken. then reinstall,APNS delivers the same devicetoken to iphone,so when a new push item comes to APNS,it successly delivers the item to the client.
my app has synchronized devicetoken records with the provider itself,but this problem comes when no launching after reinstall(if the user launch after reinstall,I can remove the register accounts under the devicetoken).how can i solve this problem?
It can not ever be delivered until you save the new device token from
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

What are the steps in implementing Apple Push Notification?

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.