我正在实现推送通知,如果应用程序位于前台,则会正确接收它们,并使用数据调用didReceiveRemoteNotification。所以我认为令牌和服务器问题都是零。 当应用程序处于后台时它变得很丑陋:我发送通知并且从未在通知中心显示为收到,而不是显示徽章。 在设置/通知/ MyApp中,一切都处于活动状态。 也许是因为我正在使用开发证书或因为Apple的沙盒问题? 任何想法将不胜感激。 谢谢
答案 0 :(得分: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();
}
答案 1 :(得分: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);
}