5

Uygulama iOS'ta Arka Planda olduğunda push bildirimi alınmadı.

Uygulama ön planda durumdayken bildirimi alabiliyorum. Ancak Uygulama arka planda olduğunda, bildirim alınmaz. Uygulama ön plan durumuna geldiğinde, sadece bildirim alınacaktır.

Benim kodudur:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { 
// Print message ID. 
NSDictionary *userInfo = notification.request.content.userInfo; 
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]); 

// Pring full message. 
NSLog(@"%@", userInfo); 

if([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) 
{ 
    NSLog(@"INACTIVE"); 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 
else if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) 
{ 
    NSLog(@"BACKGROUND"); 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 
else 
{ 
    NSLog(@"FOREGROUND"); 
    completionHandler(UNNotificationPresentationOptionAlert); 
}} 



- (void)applicationDidEnterBackground:(UIApplication *)application { 


} 

App arka plan durumundadır:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
    willPresentNotification:(UNNotification *)notification 
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 

- hiç çağrılmaz.

Uygulama Özellikleri'nde push bildirimlerini ve ayrıca arka plan modlarında uzaktan bildirimleri etkinleştirdim. Ancak, Uygulama hala bildirim almıyor.

Bazı StackOverflow sorularına başvurdum, ancak sorunu çözemedim. IOS sürüm 10 veya kodumda herhangi bir hata eklemek için bir şey var mı?

+0

Yalnızca iOS 10 veya arka planda olduğunda tüm diğer sürümler için bunu almıyor musunuz? –

+0

@AL. Sadece iOS 10 için almayan – Himanth

+0

Kodunuzu gönder ben sorunu çözeceğim. – user3182143

cevap

8

iOS 10 için, aşağıdaki 2 yöntemi çağırmamız gerekiyor. ÖNPLAN devlet için

AMAÇ devlet için

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler 
{ 
    NSLog(@"Handle push from foreground"); 
    // custom code to handle push while app is in the foreground 
    NSLog(@"%@", notification.request.content.userInfo); 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 
{ 
    NSLog(@"Handle push from background or closed"); 
    // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background 
    NSLog(@"%@", response.notification.request.content.userInfo); 
    completionHandler(); 
} 

Bundan önce, biz AppDelegate.h dosyada UserNotifications çerçeve ve ithalat eklemelisiniz

#import <UserNotifications/UserNotifications.h> 
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 
+0

Aynı problemle karşı karşıyayım. Yukarıdaki adımları takip ettim ama maalesef hala çalışmıyor. bana yardım edebilir misin? –

+0

http://stackoverflow.com/questions/43694163/how-to-get-local-notification-when-app-is-running-in-objective-c/43705956#43705956 – user3182143

+0

Tamamlama çağırmanıza gerek yok işleyicisi? –