0

我正在实现推送通知,如果应用程序在前台,它们会被正确接收,使用数据调用didReceiveRemoteNotification。所以我认为令牌和服务器问题为零。当应用程序处于后台时,它会变得丑陋:我发送通知并且从未在通知中心显示为收到,而不是显示徽章。在 Settings/Notifications/MyApp 中,一切都处于活动状态。可能是因为我使用了开发证书还是因为 Apple 的沙盒问题?任何想法将不胜感激。谢谢

4

2 回答 2

2

固定的。创建有效负载时,我使用的是简单数组,而不是带有 ['aps'] 键的数组。服务器端问题。我不知道为什么当文档说它不会时,Apple 会发送格式不正确的通知。这个细节让我觉得服务器端没问题,这就是我没有粘贴代码的原因,对此感到抱歉。

错误的:

  $payload = array('alert' => $this->code->getText(),
                   'sound' => 'default', 
                   'badge' => '1');
  $message['payload'] = json_encode($payload);

正确的:

$body['aps'] = array(
                'alert' => $this->code->getText(),
                'sound' => 'default',
                'badge' => '1'
            );
            $message['payload'] = json_encode($body);

并发送代码...

  if ($this->sendNotification($message['device_token'], 
                              $message['payload'])) {       
  } else {  // failed to deliver
      $this->reconnectToAPNS();
     }
于 2013-07-12T14:14:40.240 回答
0

根据您的描述,我假设您有时会收到 APN(Apple 推送通知)。仔细检查 AppDelegate 中的代码,看看是否有以下内容:

在你的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

你应该有

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

在您的 didReceiveRemoteNotification 中尝试此代码以查看当您收到 APN 时会发生什么:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);

NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);

NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
}

最后确保您实际上已成功注册以接收 APN:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSLog(@"My token is: %@", deviceToken);
}

包括此代码,以防您在注册 APN 时遇到错误:

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(@"Failed to get token, error: %@", error);
}
于 2013-07-11T20:20:53.293 回答