iOS Swift GCM / FCM在后台接收通知

时间:2016-08-17 17:21:07

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

关闭应用后,我无法进行通知。

我是从FCM / GCM(python服务器)发送的:

{
  "registration_ids": ["APA91bEDB9dVf-..."]
  "collapse_key": null,
  "content_available": true,
  "data": {
    "msg": "{\"model\":\"article\",\"data\":\"reload\"}"
  },
  "notification": {
    "body": "my body",
    "sound": "default",
    "title": "my title"
  },
  "priority": "high",
  "time_to_live": 3600
}

所以卷曲是这样的:

  

curl --header“授权:key = MYKEY”--header“Content-Type:   application / json“https://fcm.googleapis.com/fcm/send -d   '{“registration_ids”:[“APA91bEDB9dVf -...”],“collapse_key”:   null,“content_available”:true,“notification”:{“body”:“my   body“,”sound“:”默认“,”标题“:”我的标题“},”优先级“:   “high”,“time_to_live”:3600,“数据”:   { “msg” 中: “{\” 模型\ “:\” 文章\ “\ ”数据\“:\ ”重载\“}”}}“

终端的响应是:

  

{ “multicast_id”:6089491211057070352, “成功”:1, “失败”:0, “canonical_ids”:0 “结果”:[{ “MESSAGE_ID”: “0:1471474398816666%cc062fb9cc062fb9”}]}

iOS应用中的

Background Modes已开启且Remote notifications

enter image description here

AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    FIRApp.configure()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)

    return true
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    let notification = UILocalNotification()
    notification.alertBody = "Test"
    notification.alertAction = "open"
    notification.fireDate = NSDate(timeIntervalSinceNow: 1)
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["UUID": 12312]

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
}

但是当我发送该消息时仍然没有显示,但是当应用程序打开时,通知可以正常工作。

可能有什么问题?

1 个答案:

答案 0 :(得分:-2)

{   “到”: “ID”,   “通知”:{
    “声音”:“默认”,     “头衔”: “TITLE”     “体”: “BODY”   },   “优先”:“高” } 以此模式创建通知