我在我的一个应用程序中使用通知,当应用程序处于活动状态时,收到通知,我处理数据,一切都很好,但是当应用程序处于后台或被杀时我没有收到任何通知。问题是什么?任何一个PLZ帮助我?
谢谢!
这是我到目前为止所做的事情
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
tokenstring = [[NSString alloc] initWithFormat:@"%@",deviceToken];
tokenstring = [tokenstring stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
tokenstring = [[NSString alloc]initWithFormat:@"%@",[tokenstring stringByReplacingOccurrencesOfString:@" " withString:@""]];
NSLog(@"TokeinID:>> %@",tokenstring);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"didReceiveRemoteNotification: %@",userInfo);
AudioServicesPlaySystemSound(1002);
//Some code or logic
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"didFailToRegisterForRemoteNotificationsWithError: %@",err.description);
}
答案 0 :(得分:3)
当您收到远程通知时,只有当您的应用位于前台时才会调用-application:didReceiveRemoteNotification:
。如果您的应用程序在后台或终止,则操作系统可能会显示警报或播放声音(取决于通知中的aps字典),但不会调用委托方法。
如果使用该通知的操作按钮启动,则后台收到的远程通知将仅传递给您的应用程序,然后您需要查看-application:didFinishLaunchingWithOptions:
上的启动选项字典以查看通知的内容
如果您正在查看iOS7的新内容提取/远程通知背景支持,请检查Will iOS launch my app into the background if it was force-quit by the user?并查看是否有帮助,因为这些功能在非常具体的情况下有效。
答案 1 :(得分:1)
小代码..
当应用未运行时
(BOOL)应用程序:(UIApplication *)应用程序 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
被称为..
你需要检查推送通知
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(@"app recieved notification from remote%@",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
}