简体   繁体   中英

iOS Swift GCM/FCM receive notification in background

I can't make notification work when app is closed.

I am sending this from FCM/GCM(python server):

{
  "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
}

so curl is like this:

curl --header "Authorization: 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": "default","title": "my title"},"priority": "high","time_to_live": 3600,"data": {"msg":"{\\"model\\":\\"article\\",\\"data\\":\\"reload\\"}"}}'

and response in terminal is:

{"multicast_id":6089491211057070352,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1471474398816666%cc062fb9cc062fb9"}]}

in iOS app Background Modes is ON and Remote notifications :

在此处输入图片说明

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

But still nothing shows when i send that msg, but when app is opened notification works.

What can be problem?

{ "to":"ID", "notification":{
"sound":"default", "title":"TITLE", "body":"BODY" }, "priority": "high" } create notification in this pattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM