推送通知仅在应用程序处于后台时才有效,但是当它从背景中删除时它也无效,当它返回到后台时,我没有收到任何推送通知。我不得不删除应用程序并从testflight再次下载以便能够接收推送通知。
这是代码
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
#if !TARGET_IPHONE_SIMULATOR
//Do stuff that you would do if the application was not active
NSLog(@"remote notification: %@",[userInfo description]);
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
NSLog(@"Aps Info : %@",apsInfo);
NSString *alert = [apsInfo objectForKey:@"alert"];
NSLog(@"Received Push Alert: %@", alert);
NSString *sound = [apsInfo objectForKey:@"sound"];
NSLog(@"Received Push Sound: %@", sound);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
NSString *badge = [apsInfo objectForKey:@"badge"];
NSLog(@"Received Push Badge: %@", badge);
application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];
#endif
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSInteger pushCode = [userInfo[@"pushCode"] integerValue];
NSLog(@"Silent Push Code Notification: %i", pushCode);
NSDictionary *aps = userInfo[@"aps"];
NSString *typeID = userInfo[@"type_id"];
NSString *alertMessage = aps[@"alert"];
NSLog(@"Type ID %@",typeID);
NSArray * array =[typeID componentsSeparatedByString:@","];
NSLog(@"Item %@,Item2 %@",[array objectAtIndex:0],[array objectAtIndex:1]);
[self typeID:[array objectAtIndex:0] userID:[array objectAtIndex:1] message:alertMessage];
NSLog(@"apss%@,alert%@,%@",aps,alertMessage,userInfo);
}
答案 0 :(得分:0)
问题在于方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
}
你应该用这种方法“捕捉”到达后台的通知:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
}
fetchCompletionHandler 方法用于接收“静默”推送消息,该消息在 didReceiveRemoteNotification 之前被调用(如果已实现)。