在 iOS 中未收到推送通知消息

Posted

技术标签:

【中文标题】在 iOS 中未收到推送通知消息【英文标题】:Push notification message not received in iOS 【发布时间】:2016-04-29 13:12:08 【问题描述】:

我在我的 ios 应用中使用推送通知。

我无法从服务器获取推送通知消息。但是我可以在免费使用第三方服务器 APN Tester 时收到消息。

在我的应用程序中,我使用后端作为 .net 网络服务(即 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];
    


任何建议请。

提前致谢。

【问题讨论】:

我没有看到您在哪里添加了委托方法 didReceiveRemoteNotification 和 didReceiveRemoteNotification:completionHandler ? 您使用的是苹果生产还是开发推送服务器? 谢谢,我们正在使用开发推送服务器... 你好,我已经添加了didReceiveRemoteNotification函数...但是我不知道在哪里以及如何添加didReceiveRemoteNotification:completionHandler这个函数..你能提供这个的源代码吗.. 【参考方案1】:

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];


【讨论】:

以上是关于在 iOS 中未收到推送通知消息的主要内容,如果未能解决你的问题,请参考以下文章

在 iPhone 中未收到任何推送通知

IOS Firebase 云功能中未收到推送通知

在 IOS 中未收到 FCM 推送通知。而应用程序在后台或终止。我使用 FirebaseMessaging 6.0.9 Pub

在科尔多瓦项目中未收到解析推送通知

在android中未收到推送通知

收到消息时向 iOS 推送通知不起作用