0
我收到来自服务器的推送通知,但是我的postNotification没有被调用。它正确地输入了if语句,该语句声明类型== 1.所有用户默认值都被正确存储,但通知未被调用。我在视图控制器的函数中放置了断点以及警报,并通过后置通知调用该函数。但它永远不会被调用。这里是我的代码:未从应用程序调用发布通知didFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSString *type = [dictionary objectForKey:@"type"];
if([[NSString stringWithFormat:@"%@",type]integerValue]==1)
{
[[NSUserDefaults standardUserDefaults]setObject:[[dictionary objectForKey:@"aps"] valueForKey:@"alert"] forKey:@"message"];
[[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:@"senderid"] forKey:@"senderID"];
[[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:@"sendername"] forKey:@"sendername"];
[[NSUserDefaults standardUserDefaults]setObject:[dictionary objectForKey:@"userimage"] forKey:@"pushImage"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(@"message is %@",[[dictionary objectForKey:@"aps"] valueForKey:@"alert"]);
NSLog(@"senderID is %@",[dictionary objectForKey:@"senderid"]);
NSLog(@"sender name is %@",[dictionary objectForKey:@"sendername"]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:launchOptions];
}
}
#ifdef __IPHONE_8_0
if(NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
#endif
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeNewsstandContentAvailability;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
application.applicationIconBadgeNumber=0;
[UIApplication sharedApplication].statusBarHidden = NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible=FALSE;
return YES;
}