-2

我是 iPhone 开发的新手,但设法在我的 iOS 应用程序中接收推送通知。但是,当我刷掉传入的推送通知时,它只会打开应用程序,而不是通知的相关帖子。

这是我的代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"Eine Nachricht ist angekommen, während die App aktiv ist");

NSString* alert = [[userInfo objectForKey:@"aps"] objectForKey:@"id"];

NSLog(@"Nachricht: %@", alert);

//This is to inform about new messages when the app is active

//UIApplicationState state = [[UIApplication sharedApplication] applicationState];
//if (state == UIApplicationStateActive) {
//    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Neuer Artikel" message:@"Nachricht" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
//    [alertView show];
//    }
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Device Token=%@", deviceToken);

NSUInteger theCount = [deviceToken length];
NSMutableString *theString = [NSMutableString stringWithCapacity:2 * theCount];
unsigned char const *theBytes = [deviceToken bytes];

for(NSUInteger i = 0; i < theCount; ++i) {
    [theString appendFormat:@"%2.2x", theBytes[i]];
}

NSString* url = [NSString stringWithFormat:@"HERE_IS_MY_REGISTERING_URL",theString,theString];
NSLog(@"APNS URL : %@",url);

NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *urlResponse, NSData *data, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    }
}];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Error bei der Registrierung");

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.frame = [[UIScreen mainScreen] bounds];
[self setApplicationDefaults];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
//This is the start of the push notification settings
[self.window makeKeyAndVisible];

现在,我不知道该放什么,打开一个推送通知的相关帖子......

4

1 回答 1

5

你能指望什么?从您的代码中,我看不到您提供了有关要打开哪个帖子的任何信息。Apple、Xcode 或您的代码都不会神奇地知道这一点。

在推送通知的有效负载中,您必须提供您所指的帖子的信息,然后在您的 didReceiveRemoteNotification 中阅读此信息。

请参阅:此处的“JSON 有效负载示例”:Apple 推送通知服务

于 2012-10-30T13:12:23.510 回答