2017-01-03 122 views
0

我是iOS和Swift的新手。我正在我的應用程序中實施遠程通知。一切正常,當應用程序處於活動狀態或在後臺。但是,當應用程序終止時,我的通知不會顯示出來。我所得到的只是通知的警報聲。應用終止時的遠程通知

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    if #available(iOS 10.0, *){ 
    }else{ 
     let notification = UILocalNotification() 
     notification.alertTitle = "my Title" 
     notification.alertBody = "My Message" 
     notification.category = "customCategory" 
     notification.soundName = UILocalNotificationDefaultSoundName 
     application.scheduleLocalNotification(notification) 
    } 
} 

回答

-1

下面的AppDelegate方法將被調用當您收到通知,並應用在滅活狀態

didFinishLaunchingWithOptions 

所以,你應該從這裏妥善處理當應用程序處於滅活狀態。

下面的代碼有助於識別didFinishLaunchingWithOptions方法遠程/推或本地通知:

if let launchOpts = launchOptions as [UIApplicationLaunchOptionsKey: Any]? { 
      if let notificationPayload = launchOpts[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary { 

       //Handle push notification here 
       } 
else if let notification = (launchOpts as NSDictionary).object(forKey: "UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification { 
       //Handle local notification here 
       } 
+0

你可以建議我一些例子?並且在應用程序啓動時調用didFinishLaunchingWithOptions方法。它是如何幫助我在應用程序未運行時顯示通知的? –

相關問題