0

Auf iOS10, wenn eine App geöffnet ist, erhalte ich Push-Benachrichtigungen, aber wenn ich die Home-Taste drücke und die App im Hintergrundmodus ist, erhält sie nichts.Warum erhält ein Gerät keine Push-Benachrichtigung im Hintergrund?

Mein Code ist:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate { 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
if #available(iOS 10.0, *) { 
     let authOptions: UNAuthorizationOptions = [.Alert, .Badge, .Sound] 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Sound, .Alert, .Badge], completionHandler: { (granted: Bool, error: NSError?) in 

      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.currentNotificationCenter().delegate = self 
      // For iOS 10 data message (sent via FCM) 
      FIRMessaging.messaging().remoteMessageDelegate = self 
     }) 
    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 

// Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 

    application.registerForRemoteNotifications() 

} 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
    let userInfo = notification.request.content.userInfo 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
    //Handle the notification 
    print("User Info => ",response.notification.request.content.userInfo) 
    completionHandler() 
} 

So kann ich nicht finden, wo ist das Problem? Irgendeine Idee? Oder gibt es etwas Neues in iOS10?

drehte ich mich schon auf diese Funktionen:

enter image description here enter image description here

+0

func Anwendung (Anwendung: UIApplication, didReceiveRemoteNotification userinfo: [NSObject: ANYOBJECT], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { completionHandler (.NewData) } brauchen – dragoneye

Antwort

1

Haben Sie versucht, diese

enter image description here

Bitte bestätigen

enter image description here und diese

tun das nach tun se, werde ich diese Antwort bearbeiten entsprechend

+0

Hintergrund holen ja, das habe ich das schon. Ich werde meine Frage jetzt aktualisieren –

+0

bereits aktualisiert –

+0

ist die .pem u erzeugt ordnungsgemäß funktioniert? Bitte verwenden Sie das Terminal, um zu prüfen, ob ein korrekter SSL-Handshake auf dem Produktions-/Sandbox-Server vorhanden ist. Obwohl sehr unwahrscheinlich coz Sie Push bekommen, während app im Vordergrund ist, immer noch gut, um die Schritte –

0

für iOS 10 mit diesen Schritten versuchen,

  • Vor allem in den Ziele -> Funktionen -> aktivieren Push Notifications Push Notifications Entitlements hinzufügen .

  • Zweite ein Implementieren UserNotifications.framework in Ihre App. Importieren Sie UserNotifications.framework in Ihrem AppDelegate.

    Import < UserNotifications/UserNotifications.h>

    @interface AppDelegate: UIResponder < UIApplicationDelegate, UNUserNotificationCenterDelegate>

    @end

  • Dritter Schritt - Innen didFinishLaunchingWithOptions Methode UIUserNotificationSettings zuweisen und implementieren Sie den UNUserNotificationCenter-Delegaten.

    definieren SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO (v) ([[[UIDevice currentDevice] Systemversion] vergleichen: v Optionen: NSNumericSearch] = NSOrderedAscending!)

    - (BOOL) Anwendung: (UIApplication *) Anwendung didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {

    if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){ 
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
        center.delegate = self; 
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ 
         if(!error){ 
          [[UIApplication sharedApplication] registerForRemoteNotifications]; 
         } 
        }]; 
    } 
        return YES; 
    } 
    
    • Letzter Schritt - Umsetzung dieser zwei Delegatmethoden.

// ==================== Für iOS 10 ============= =======

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

    //Called when a notification is delivered to a foreground app. 

    NSLog(@"Userinfo %@",notification.request.content.userInfo); 

    completionHandler(UNNotificationPresentationOptionAlert); 
} 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 

    //Called to let your app know which action was selected by the user for a given notification. 

    NSLog(@"Userinfo %@",response.notification.request.content.userInfo); 

} 

Ich hoffe, es wird Ihnen helfen.

Danke.

Verwandte Themen