[英]iOS Swift GCM/FCM receive notification in background
应用关闭后,我无法使通知正常工作。
我正在从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是这样的:
curl --header“授权:密钥= MYKEY” –header“内容类型: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“},”优先级“:” high“,” time_to_live“:3600,” data“:{” msg“:” {\\“ model \\”:\\“ article \\”,\\“ data \\”:\\“ reload \\”}“}} '
终端的响应是:
{“ multicast_id”:6089491211057070352,“ success”:1,“ failure”:0,“ canonical_ids”:0,“ results”:[{“ message_id”:“ 0:1471474398816666%cc062fb9cc062fb9”}]}
在iOS应用程式的[ Background Modes
中开启,并显示[ 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)
}
但是当我发送该消息时,仍然没有任何显示,但是当应用程序打开时,通知工作正常。
有什么问题吗?
{“ to”:“ ID”,“ notification”:{
“ sound”:“默认”,“ title”:“ TITLE”,“ body”:“ BODY”},“ priority”:“ high”}以这种模式创建通知
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.