ios - 应用程序在后台时不显示推送通知

标签 ios swift firebase push-notification apple-push-notifications

我正在尝试在我的 IOS 应用程序中实现 Firebase 推送通知,但无法弄清楚当应用程序处于后台时如何接收通知。

我使用 print 来显示通知,但它只在应用程序打开时打印通知。如果我在应用程序处于后台时发送通知,什么也不会发生,但消息会在我重新打开应用程序时立即打印出来。

下面是我的 AppDelegate 代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    registerForPushNotifications(application)
    FIRApp.configure()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter
        .defaultCenter()
        .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
                     name: kFIRInstanceIDTokenRefreshNotification, object: nil)

    // Override point for customization after application launch.
    return true
}

func registerForPushNotifications(application: UIApplication) {
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()
}


func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    sendNotification();

    print("===== didReceiveRemoteNotification ===== %@", userInfo)
}




func tokenRefreshNotificaiton(notification: NSNotification) {
    let refreshedToken = FIRInstanceID.instanceID().token()!
    print("InstanceID token: \(refreshedToken)")
    connectToFcm()
}

func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}
func sendNotification() {
    let notification = UILocalNotification()
    let dict:NSDictionary = ["ID" : "your ID goes here"]
    notification.userInfo = dict as! [String : String]
    notification.alertBody = "title"
    notification.alertAction = "Open"
    notification.fireDate = NSDate()
    UIApplication.sharedApplication().scheduleLocalNotification(notification)


}


}

我也已经添加到我的 info.plist

<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>UIBackgroundModes</key>
<array>
    <string>remote-notification</string>
</array>

这是我发送的消息的格式

{
  "to" : "",
  "notification" : {
  "body" : "",
  "title" : "",
},
  "content_available": true,
  "priority": "high"
}

最佳答案

当您的应用程序关闭时,您无法接收推送通知数据。您应该将消息存储到服务器,而不是用户从那里访问消息

关于ios - 应用程序在后台时不显示推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063355/

相关文章:

ios - 将 Core Data 对象从 tableViewController 传递到 TabBarController

java - Swift 中的高级 Java 枚举

android - 如何在android后台服务中监听firebase事件?

ios - Swift + Firebase 模拟登录报错

iOS 共享扩展不适用于图片网址

ios - 使用 Swift 生成 PDF

swift - 编辑按钮没有任何功能,单元格左侧没有带减号的红色圆圈

android - Firebase 动态链接无法获取参数

iphone - 在 IOS 5 中检测设备是否处于静音模式?

ios - EXC_BAD_ACCESS 关于观察 TableView contentSize 值