我在iOS应用中使用推送通知。
我无法从服务器获取推送通知消息。但是我可以免费使用第三方服务器APN Tester时收到消息。
在我的应用程序中,我使用后端作为.net webservice(即asmx文件)。以下是我用于推送通知的代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
/-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
NSString *deviceTokenString = [NSString stringWithFormat:@"%@", deviceToken];
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken");
NSLog(@"My token is: %@", deviceTokenString);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:deviceTokenString
message:@"devicetoken"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
/
}*/
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
_token_data =deviceToken;
NSString *title = [NSString stringWithFormat:@"%@",_token_data];
NSString *trimmedString = [title stringByReplacingOccurrencesOfString: @" " withString:@""];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString: @"<" withString:@""];
trimmedString = [trimmedString stringByReplacingOccurrencesOfString: @">" withString:@""];
_tokenkey = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
NSString *soapFormat = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http:/
"<soap:Body>\n"
"<AddAppleDeviceToken xmlns=\"http:/
"<PortalId>0</PortalId>\n"
"<ModuleId>12</ModuleId>\n"
"<strDeviceToken>sdfssdfs</strDeviceToken>\n"
"</AddAppleDeviceToken>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.mywebsite.com/push.asmx"
wsDeviceToken.asmx"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapFormat length]];
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http:/
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
/
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
NSLog(@"soapFormat %@",soapFormat);
if (connect) {
_webData = [[NSMutableData alloc]init];
NSLog(@"Connection Establish");
}
else {
NSLog(@"No Connection established");
}
NSString * test = [NSString stringWithFormat:@"Your Device token is %@", trimmedString];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:test
message:@"devicetoken"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
if (notificationSettings.types != UIUserNotificationTypeNone) {
NSLog(@"didRegisterUser");
[application registerForRemoteNotifications];
UIAlertView *alert33 = [[UIAlertView alloc] initWithTitle:@"didRegisterUser"
message:@"didRegisterUser"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert33 show];
}
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
/
/
NSString *message = [[userInfo objectForKey:@"aps"]
objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@""
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
/
}
#pragma - NSURLConnection delegate method
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_webData setLength: 0];
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"HTTP error %zd", (ssize_t) httpResponse.statusCode);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{ NSLog(@"DONE. Received Bytes: %lu", (unsigned long)[_webData length]);
self.xmlParser = [[NSXMLParser alloc]initWithData:_webData];
[self.xmlParser setDelegate: self];
[self.xmlParser parse];
}
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"AddAppleDeviceTokenResult"]) {
NSString *tmpstr = [[NSString alloc] init];
tmpstr = [tmpstr stringByAppendingString:soapResults];
[soapResults setString:@""];
NSLog(@"AddAppleDeviceTokenResult %@",tmpstr);
if([tmpstr isEqualToString:@"DeviceToken Added in Database"])
{
NSString * test = [NSString stringWithFormat:@"Your Device token is added in database"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:test
message:_tokenkey
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Token added successfully");
[[NSUserDefaults standardUserDefaults]setValue:@"1" forKey:@"isDeviceTokenAdded"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (!soapResults) {
/
soapResults = [[NSMutableString alloc] init];
NSLog(@"soapResults %@",soapResults);
}
if([string length] > 0){
NSString *cleanString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([cleanString length] >0){
[soapResults appendString: string];
}
}
}
任何建议请。
提前致谢。
答案 0 :(得分:0)
jejai 替换此方法
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = [[userInfo objectForKey:@"aps"]
objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@""
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
要
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler
{
NSString *message = [[userInfo objectForKey:@"aps"]
objectForKey:@"alert"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@""
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}