我正在用expo开发一个应用程序(SDK 48)。我正在尝试收听iPhone 11(真实的设备)后台收到的通知。
我已经把它添加到我的App中了。js文件:
const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK';
TaskManager.defineTask(
BACKGROUND_NOTIFICATION_TASK,
({ data, error, executionInfo }) => {
if (error) {
report.log("error occurred");
}
console.log('Notification in background received: ' + JSON.stringify(data))
let newNotification = new MyNotification(
getFormattedDate(new Date()),
data?.notification.data.title || 'Title',
data?.notification.data.message || 'Message'
)
saveNotification(newNotification).then(() => store.dispatch(setNewMessageStatus(true))).catch(err => console.log('Error saving notification ' + err));
}
);
Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);
Expo Notification documentation说道:“要在应用程序在iOS上后台运行时处理通知,您必须将远程通知添加到iOS。infoPlist.UIBackgroundModes键。json,并添加“content-available”:1到您的推送通知有效负载。在正常情况下,“内容可用”标志应该在应用没有运行并且没有被用户杀死的情况下启动应用,但是,这最终由操作系统决定,所以它可能并不总是发生。“
这是我的应用程序中的“iOS”键。json:
"ios": {
"supportsTablet": true,
"googleServicesFile": "./GoogleService-Info.plist",
"bundleIdentifier": "com.gsjardim83.foodfreedomRN",
"infoPlist":{
"UIBackgroundModes":["remote-notification"]
}
},
但我不知道如何设置“内容可用”:1使用expo push API。
当应用程序仅在前台时调用我的侦听器,而不是在后台。当我查看Apple的文档时,他们给予了更多关于content-available密钥应该放在哪里的细节:https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
有没有办法通过expo push API来做到这一点?我想避免使用APN服务器来保持简单。
我尝试向Expo Push API发送以下请求:
{
"to": "ExponentPushToken[xxxxxxxxxx]",
"sound": "default",
"body": "Message in the background 3",
"badge": 1,
"title": "Food freedom App",
"priority": "high",
"data": {
"trigger": {
"payload": {
"aps": {
"content-available": 1
}
}
}
}
}
但我收到的是(我只能在与通知交互时看到这个,因为onReceivedListener没有被调用):
{
"actionIdentifier":"expo.modules.notifications.actions.DEFAULT",
"notification":{
"date":1679656100.9144022,
"request":{
"content":{
"summaryArgumentCount":0,
"targetContentIdentifier":null,
"threadIdentifier":"",
"attachments":[
],
"categoryIdentifier":"",
"summaryArgument":null,
"data":{
"trigger":{
"payload":{
"aps":{
"content-available":1
}
}
}
},
"title":"Food freedom App",
"subtitle":null,
"badge":1,
"launchImageName":"",
"sound":"default",
"body":"Message in the background 2"
},
"identifier":"CD4C4301-3696-4FA6-A080-333E59B42A8B",
"trigger":{
"payload":{
"body":{
"trigger":{
"payload":{
"aps":{
"content-available":1
}
}
}
},
"aps":{
"sound":"default",
"badge":1,
"alert":{
"body":"Message in the background 2",
"launch-image":"",
"title":"Food freedom App",
"subtitle":""
},
"category":"",
"thread-id":""
},
"scopeKey":"@gsjardim83/foodfreedomRN",
"projectId":"xxxxxxxxxx",
"experienceId":"@gsjardim83/foodfreedomRN"
},
"type":"push",
"class":"UNPushNotificationTrigger"
}
}
}
}
我也试过其他格式,但可用内容不显示在“payload”的“aps”键下
1条答案
按热度按时间qnakjoqk1#
你可以将
_contentAvailable: true
添加到你的payload中,它看起来像这样:{ to: <push_token>, "_contentAvailable": true}