我从 @aws-amplify/pushnotification 设置 PushNotification.onNotification,如下所示:
if (notification.foreground) {
console.log('notification received in foreground ', notification);
} else {
console.log('notification received in background ', notification);
}
if (PushNotificationIOS !== undefined) {
notification.finish(PushNotificationIOS.FetchResult.NoData);
}
});
在 Android 上运行良好。在 iOS 上,尽管我收到了通知,但无法触发 onNotification/onNotificationOpened 事件。有一种特殊情况,当我通过 Pinpoint -> Test Messaging 发送“标准消息”时,它实际上触发了该功能,当我记录通知时,它看起来像这样
'notification received in background ',
{ _data:
{ remote: true,
notificationId: '45BAA2A6-8676-402E-8E6B-03A69173AC8C' },
_remoteNotificationCompleteCallbackCalled: false,
_isRemote: true,
_notificationId: '45BAA2A6-8676-402E-8E6B-03A69173AC8C',
_alert: { title: ' test', body: 'qwerty' },
_sound: undefined,
_badgeCount: undefined,
_category: undefined,
_contentAvailable: 1,
_threadID: undefined }
(通知是在前台收到的,但消息不包含该信息,因此它作为背景打印。使用Pintpoint -> 测试消息 -> 标准消息时,前台/后台都会触发该事件)。 Pinpoint Raw 消息也不起作用。 SNS 相同/自定义负载不起作用。
我已经根据以下内容增强了 AppDelegate https://github.com/react-native-push-notification-ios/push-notification-ios#augment-appdelegate
有人知道 amplify 是否需要特定的有效负载才能触发事件?或者如果有其他原因导致这个..?
SNS自定义结构
"APNS_SANDBOX": "{\"aps\":{\"alert\":\"Sample message for iOS development endpoints\"}}"
}
精确定位原始消息
{
"APNSMessage": {
"aps": {
"alert": ""
}
},
"GCMMessage": {
"data": {
"message": ""
}
},
"ADMMessage": {
"data" : {
"message": ""
}
},
"BaiduMessage": {
"title":"",
"description": ""
}
}
解决方案是添加内容可用和附加数据,以访问 amplify PushNotification 的 iOS 中的标题、正文和数据。
{
aps: {
alert: {
title: "title",
body: "message"
},
"content-available": "1",
key1: "value",
key2: "value",
},
}
字符串化
"{\"aps\":{\"alert\":{\"title\":\"title\",\"body\":\"message\"},\"content-available\":\"1\",\"key1\":\"value\",\"key2\":\"value\"}}"
要使用 iOS、Amplify、SNS 添加到 Henrik 的答案:
以下内容在后台(显示通知)和前台(不显示通知,但使用有效标题和正文内容调用handleNotificationReceivedInForeground())均有效。
此外,在后台时,只要包含“content-available”:“1”,就会调用handleNotificationReceivedInBackground()。
这是发送到选择“每个传输协议的自定义负载”的主题时的完整 JSON。
{ "default": "示例后备消息", "APNS_SANDBOX": "{"aps":{"alert":{"title":"我的标题","body":"正文"},"content-available":"1"}}" }
注意 - Henrik 提供的非字符串化版本对我不起作用。此外,SNS 提供的默认示例并不像 Henrik 在他的第一条消息中所说的那样工作。而且我无法获得任何类型的原始消息(使用“所有交付协议的相同有效负载”)来工作。
当失败时,我经常收到一条通知消息 - 但所有字段(正文、标题等)都是“未定义”。
代码:
const handleNotificationReceivedInForeground: OnNotificationReceivedInForegroundInput =
(notification: PushNotificationMessage) => {
console.log("PUSH - Notification received in foreground:", notification);
console.log(
"PUSH - Full notification object:",
JSON.stringify(notification, null, 2)
);
};
const handleNotificationReceivedInBackground: OnNotificationReceivedInBackgroundInput =
async (notification) => {
console.log("PUSH - Notification received in background:", notification);
// Add custom behavior for handling the notification here
};
// Event listeners
const foregroundNotificationListener:
OnNotificationReceivedInForegroundOutput =
onNotificationReceivedInForeground(
handleNotificationReceivedInForeground);
const backgroundNotificationListener =
onNotificationReceivedInBackground(
handleNotificationReceivedInBackground);
也在 AppDelegate.mm 中:
#import "AmplifyPushNotification.h"
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[AmplifyPushNotification didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
[AmplifyPushNotification didReceiveRemoteNotification:userInfo withCompletionHandler:completionHandler];
}
最后,当我的手机在深夜打开睡眠模式时,我确实被抓住了 - 并且通知停止显示!哈哈