0
我在iOS應用中使用推送通知。iOS中未收到推送通知消息
我無法從服務器獲取推送通知消息。但是,我可以免費使用第三方服務器APN測試儀時收到消息。
在我的應用程序中,我使用.net web服務(即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];
}
}
}
任何suggessions請。
在此先感謝。
我沒有看到你所添加的委託方法didReceiveRemoteNotification和didReceiveRemoteNotification:completionHandler? – SeanLintern88
您是否在使用Apple生產或開發推送服務器? – Gruntcakes
感謝,我們使用的開發推送服務器... – jejai