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;
}