为什么设备无法在后台接收推送通知?

时间:2016-12-19 12:00:14

标签: ios swift push-notification apple-push-notifications firebase-cloud-messaging

在iOS10上打开应用程序时,我接收到推送通知,但是当我按下主页按钮并且应用程序处于后台模式时 - 它没有收到任何内容。

我的代码是:

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()
}

所以我找不到问题所在?任何的想法?或者iOS10中有新的东西?

我已经开启了这些功能:

enter image description here enter image description here

2 个答案:

答案 0 :(得分:1)

你试过这个吗

enter image description here  这个

enter image description here

请在完成这些后确认,我会相应地编辑这个答案

答案 1 :(得分:0)

尝试使用iOS 10的此步骤

  •   

    首先在目标 - >能力 - >启用推送通知以添加推送通知权利。

  •   

    第二个在您的应用中实施 UserNotifications.framework 。在 AppDelegate 中导入UserNotifications.framework。

    import< UserNotifications / UserNotifications.h>

    @interface AppDelegate:UIResponder< UIApplicationDelegate,UNUserNotificationCenterDelegate>

    @end

  •   

    第三步 - 在didFinishLaunchingWithOptions方法内部分配UIUserNotificationSettings并实现UNUserNotificationCenter委托。

    定义SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)([[[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch]!= NSOrderedAscending)

    - (BOOL)应用程序:(UIApplication *)应用程序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;
    }
    
    •   

      最后一步 - 实现这两个委托方法。

// ====================对于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);

}

我希望它会对你有所帮助。

感谢。