【发布时间】:2018-09-05 00:56:11
【问题描述】:
我正在使用 FCM 将推送通知发送到单个 iOS 设备,由对 Firebase Cloud Functions 的 http 请求触发。遵循 FCM 和 APNs 文档后,当应用程序在后台运行时,一切正常。我在所有情况下都成功收到了通知。
唯一的问题:推送通知没有点亮我的睡眠状态的锁屏。 我仍然会收到推送通知,并且当我按下主页按钮手动唤醒设备时可以看到它。但通知本身不会唤醒我的设备。
如何复制
- 打开应用
- 按主页按钮(现在应用程序在后台)
- 锁定手机
- 发送推送通知
- 收到通知但未点亮锁屏。
我已打开“后台模式”功能 > 选择了“远程通知”和“后台获取”。我的部署目标是 10.0。我的应用允许锁定屏幕通知。
(大多数类似问题的答案都是“打开后台模式”或“将内容可用设置为 1”,我都做了。)
我将有效负载发送到 APNs 的云函数:
exports.pushMatchNotif = functions.https.onRequest((req, res) => {
let receiverToken = req.receiverToken
var msg = {
'apns': {
'header': {
'apns-priority': '10'
},
'payload': {
'aps': {
'alert': {
'title': 'Hi',
'body': 'Hello',
},
'badge': 2,
'sound': 'default'
'content-available': 1
}
}
},
'token': receiverToken
};
admin.messaging().send(msg)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
let json = {"Response": response}
res.send(json)
})
.catch((error) => {
console.log('Error sending message:', error);
let json = {"Error": error}
res.send(json)
});
})
我的 didFinishLaunchingWithOptions 方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge , .sound]) {
(granted, error) in
print("granted is \(granted)")
}
application.registerForRemoteNotifications()
return true
}
我的 didReceiveRegistrationToken 方法(FCM 委托方法):
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase deviced token: \(fcmToken)")
}
我的 didReceiveRemoteNotification 方法:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
}
我错过了什么?
【问题讨论】:
-
您的设备运行的是 iOS 10+,对吧?
-
是的,它正在运行 11+
-
您找到解决方案了吗?我为同样的痛苦。
标签: ios firebase apple-push-notifications firebase-cloud-messaging