user2239655
user2239655

Reputation: 840

APNS push notification from server

I have web app written in JAVA and I would like to push notifications from my service. I can't use local notifications from browser because I'm targeting on iOS (tablets). So I would like to use a APNs. If I would like to send message to device I need token from this device;). So my question is.

  1. How Can I get this token?

Edit: I have't precise one thing. It is possible to get device's token without any native solution. For example tablet connect to out site, do something and we want to answer useing notification. And how to get token to send this message.

Upvotes: 0

Views: 930

Answers (3)

Paulw11
Paulw11

Reputation: 114865

Unfortunately, it isn't possible to use APNS without a native app on the iOS device. The push notification token is unique for each app on each device - so a token obtained from one app cannot be used with another app on the same device (There is an exception to this, where apps from the same developer can share a token).

Further the push notification service relies upon a digital certificate that is managed by the developer using the Apple developer portal.

Finally, when a push notification is sent to the iOS device, it is actually delivered to the corresponding app for processing - so if there is no native app on the device, there is nothing to process the notification.

You can get the detail on APNS programming in Apple's programming guide

Upvotes: 1

2intor
2intor

Reputation: 1044

In order to get this device token ios application will have to request for registering push notification service,this will result in a dialog asking for permission that this application wants to send you notification if iOS user says yes then device will register for push notification and will be provided a device token.

This device token has to be sent to server by the ios app developer then only server can use this device token and send it to APNS.

to register for push notification from ios device use below method,

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge];

if ios user allows says yes then below method will be called which will give you device token which needs to be sent to the server for posting notification from server.

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  NSString * deviceTokenstring = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"device token :%@",deviceTokenstring);
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:REMOTENOTIFICATION_URL,deviceTokenstring]]] delegate:self];
[urlConnection start];

}

Upvotes: 0

Gajendra Rawat
Gajendra Rawat

Reputation: 3663

You can get device token from this method. put this method in your appdelegate.m class.make sure you are running your iOS app in real device if you are running app in virtual device(Simulator),then you can not find device token,

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"content---%@", token);
} 

Upvotes: 0

Related Questions