我正在我的应用程序中实现推送通知,一切似乎都很好,但我的设备没有收到推送通知。
当从上面的 URL 发出通知时,我得到了响应;
“连接到 APNS 消息已成功传递”。
但在那之后,我的设备上没有任何通知。请建议我如何解决这个问题。
我正在我的应用程序中实现推送通知,一切似乎都很好,但我的设备没有收到推送通知。
当从上面的 URL 发出通知时,我得到了响应;
“连接到 APNS 消息已成功传递”。
但在那之后,我的设备上没有任何通知。请建议我如何解决这个问题。
我得到了同样的错误,因为我写错了设备令牌请检查您的设备令牌@“< XXx XXXX xxx..>”更改为@“XXX XXX..”或按照步骤操作
应用内委托
在didlaunch
// Register for sound and alert push notifications.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
// Check if the app was launched in response to the user tapping on a
// push notification. If so, we add the new message to the data model.
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
// [self addMessageFromRemoteNotification:dictionary updateUI:NO];
}
}
#pragma mark -
#pragma mark Apple Push notifications
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
// We have received a new device token. This method is usually called right
// away after you've registered for push notifications, but there are no
// guarantees. It could take up to a few seconds and you should take this
// into consideration when you design your app. In our case, the user could
// send a "join" request to the server before we have received the device
// token. In that case, we silently send an "update" request to the server
// API once we receive the token.
NSString* oldToken = [self deviceToken];
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"My token is: %@", newToken);
[self setDeviceToken:newToken];
// If the token changed and we already sent the "join" request, we should
// let the server know about the new device token.
// if ([dataModel joinedChat] && ![newToken isEqualToString:oldToken])
// {
// [self postUpdateRequest];
// }
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
// If we get here, the app could not obtain a device token. In that case,
// the user can still send messages to the server but the app will not
// receive any push notifications when other users send messages to the
// same chat room.
NSLog(@"Failed to get token, error: %@", error);
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
// This method is invoked when the app is running and a push notification
// is received. If the app was suspended in the background, it is woken up
// and this method is invoked as well. We add the new message to the data
// model and add it to the ChatViewController's table view.
NSLog(@"Received APNS notification: %@", userInfo);
[self log:[NSString stringWithFormat:@"Received APNS notification: %@", userInfo]];
UIAlertView* nofity = [[ UIAlertView alloc]initWithTitle:@"APNS" message:@"Test Successfull" delegate:nil
cancelButtonTitle:@"ok" otherButtonTitles: nil];
[nofity show];
// [self addMessageFromRemoteNotification:userInfo updateUI:YES];
}
- (NSString*)deviceToken
{
return [[NSUserDefaults standardUserDefaults] stringForKey:@"DeviceTokenKey"];
}
- (void)setDeviceToken:(NSString*)token
{
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceTokenKey"];
}
- (NSString*)udid
{
UIDevice* device = [UIDevice currentDevice];
return [device.uniqueIdentifier stringByReplacingOccurrencesOfString:@"-" withString:@""];
}
从上面的代码中获取设备令牌。
测试 将设备令牌传递给Push me baby 应用程序
检查以下
您是否获得推送令牌。如果是,请从推送令牌中删除空格和 <、>。
检查设备是否连接到互联网
检查是否在设置 -> 通知 -> 您的应用程序 -> 选择横幅或警报(弹出窗口)中启用了通知(横幅或弹出窗口)。
最有可能的是,这将是一个问题。
首先检查您的有效负载,它描述了您的推送通知的性质,这意味着它应该是短信还是警报或应用程序图标上的徽章。所以我们需要在JSON 的格式中正确设置它。先看看再说
然后
浏览 Ray Wenderlich 论坛的这个不错的教程,你肯定会发现一些遗漏的领域或步骤,
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
一个简单的技巧:我遇到了同样的问题,仔细检查了所有内容,重新创建了 pem 等,但是在我的最后一个开发证书超时并重新创建它之后无法让它继续运行。在应用程序运行数月之前。
最后,真正帮助的是从手机上擦除它并重新安装它。似乎某些存储的数据以某种方式损坏了...
请记住,该应用程序随后将使用新令牌进行注册。