[英]Local notifications are not working on device but working on simulator
我已經閱讀了一些如何使用UILocalNotification
的指南。 所以自從我第一次嘗試以來,我已經嘗試過並且沒有成功。 要在AppDelegate.m中注冊通知,我使用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
//if it is iOS 8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // if iOS 7
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
return YES;
}
並在應用程序運行時收到通知:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"notification recieved %@",notification.alertBody);
// NSLog(@"notification userinfo %@",notification.userInfo);
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSLog(@"notification fired in foreground");
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Notification!"
message:notification.alertBody
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
} else if (state == UIApplicationStateInactive){
NSLog(@"notification fired in inactive");
} else if (state == UIApplicationStateBackground){
NSLog(@"notification fired in background");
}
}
一切都在模擬器和設備上都可以 - 當應用程序運行時,我得到了我的重復通知。 但我需要在應用程序未運行時收到通知, 即應用程序處於非活動狀態時(主頁按下一次)
問題是,如果我按下按鈕按下我的通知,例如,我會毫無問題地得到它,它將顯示在通知中心,徽章將添加到我想要的應用程序圖標上。 但我希望只在我的AFNetworking
任務結束時收到通知。
要設置通知,我使用以下方法:
-(void)getProgress{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *token = [defaults objectForKey:@"token"];
NSString *header = [NSString stringWithFormat:@"Bearer %@",token];
NSDictionary *params = @{@"lang": @"en",@"project":projId};
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:header forHTTPHeaderField:@"Authorization"];
[manager POST:@"http://example.com/api/get-progress" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([[responseObject objectForKey:@"result"]isEqualToString:@"success"]){
progCreate = [responseObject objectForKey:@"progress"];
if ([progCreate intValue]==100){
//shedule notification for immediately showing
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
localNotification.alertBody = [NSString stringWithFormat:@"Notification text!"];
localNotification.alertAction = @"Show";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[localNotification setSoundName:UILocalNotificationDefaultSoundName];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// [[UIApplication sharedApplication]presentLocalNotificationNow:localNotification]; the same result as above
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(@"Error: %@", error);
}];
}
主要問題是此方法僅適用於iOS版本8.0的模擬器,並且不適用於iOS 7.1.1的iPhone 4和iOS 8.1的iPhone 5c,它也不適用於iOS 7.1的模擬器。 在iOS 7.1+的真實設備和模擬器上,我從來沒有注意到在后台接收通知,只有在我打開應用程序時才會得到它(然后它會顯示我在AppDelegate.m
設置的警報)我很高興任何建議和任何幫助。
我看到你正在使用Objective-C。 考慮到一年前問過這個問題,你可能要么想出這個問題,要么你現在正在使用Swift 2.0。 我有同樣的問題,我可以通過添加以下代碼解決它與Swift 2.0。
AppDelegate.swift :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
application.beginBackgroundTaskWithName("showNotification", expirationHandler: nil)
return true
}
你可以在這里看到我的完整答案。
您在iOS 8.0+模擬器中收到通知的事實是iOS模擬器運行時的限制。 在設備上,當您的應用在后台時,它實際上並未運行,任務將被暫停。 在iOS模擬器中,任務繼續在后台運行。
有時我們不會立即收到本地通知,但在1小時后我幾乎得到通知。 所以,等待一段特定的時間然后再檢查一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.