49
loading...
This website collects cookies to deliver better user experience
aws sns create-platform-application \
--name dev.foo-project.foo-mobile-devices.sns-app \
--platform GCM \
--attributes PlatformCredential=<Your FCM ServerKey>
aws sns create-topic --name dev__foo-project__push-notification__sns-topic
echo '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "iot.amazonaws.com"
},
"Action": [
"sts:AssumeRole"
]
}
]
}' > trust-policy.json
echo '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "arn:aws:sns:<your_region>:<your_account_id>:dev__foo-project__push-notification__sns-topic"
}
]
}' > policy.json
aws iam create-role --role-name dev.foo-project.iot-rule-action.role --assume-role-policy-document file://trust-policy.json
aws iam put-role-policy --role-name dev.foo-project.iot-rule-action.role --policy-name dev.foo-project.iot-rule-action.policy --policy-document file://policy.json
echo '{
"Sql": "SELECT concat('{\"priority\":\"high\",\"notification\":{\"title\":\"', title ,'\",\"body\":\"', body ,'\",\"mutable_content\":true,\"sound\":\"Tri-tone\",\"badge\":1,\"click_action\":\"foo\"},\"data\":{}}') as GCM, '' as default FROM 'dev.foo-project/push-notification'",
"Actions": [
{
"Sns": {
"MessageFormat": "JSON",
"TargetArn": "arn:aws:sns:<your_region>:<your_account_id>:dev__foo-project__push-notification__sns-topic",
"RoleArn": "arn:aws:iam::<your_account_id>:role/dev.foo-project.iot-rule-action.role"
}
}
]
}' > rule-payload.json
aws iot create-topic-rule \
--rule-name dev__foo_project__push_notification__iot_rule \
--topic-rule-payload file://rule-payload.json
{ "title": "...", "body": "..." }
to a valid FCM/GCM payload.import Amplify, { Auth } from "aws-amplify";
import { Authenticator, SignIn } from "aws-amplify-react";
Amplify.configure({
aws_project_region: 'your_region',
aws_cognito_identity_pool_id: 'your_cognito_identity_pool_id',
aws_cognito_region: 'your_region',
aws_user_pools_id: 'your_user_pool_id',
aws_user_pools_web_client_id: 'your_user_pool_client_id',
aws_appsync_authenticationType: "AMAZON_COGNITO_USER_POOLS",
oauth: {},
});
Auth.configure({
authenticationFlowType: "USER_PASSWORD_AUTH",
});
Amplify.Logger.LOG_LEVEL = "VERBOSE";
const AppWithAuth: React.FC = () => {
const [isSignedIn, setSignedIn] = useState(false);
return (
<Authenticator
onStateChange={(state) => {
if (state === "signedIn") {
setSignedIn(true);
}
}}>
<SignIn />
<App ready={isSignedIn} />
</Authenticator>
)
}
import { SNSClient } from "@aws-sdk/client-sns";
import { IoTClient } from "@aws-sdk/client-iot";
const amplifyCredentialProvider = async () => {
const awsCredentials = await Auth.currentCredentials();
return Auth.essentialCredentials(awsCredentials);
};
export const iotClient = new IoTClient({
region: config.region,
credentials: amplifyCredentialProvider,
});
export const snsClient = new SNSClient({
region: config.region,
credentials: amplifyCredentialProvider,
});
import { ActionPerformed, PushNotifications, PushNotificationSchema, Token } from "@capacitor/push-notifications";
import { CreatePlatformEndpointCommand, SubscribeCommand } from "@aws-sdk/client-sns";
const App: Rect.FC<{ready: boolean}> = ({ ready }) => {
useEffect(() => {
if (ready) {
// Retrieve FCM Token
PushNotifications.addListener("registration", async (token: Token) => {
const endpoint = await snsClient.send(
new CreatePlatformEndpointCommand({
PlatformApplicationArn: config.snsPlatformApplicationArn,
Token: token.value,
})
);
const subscriptionResult = await snsClient.send(
new SubscribeCommand({
Protocol: "application",
TopicArn: config.snsTopicArn,
Endpoint: endpoint.EndpointArn,
})
);
});
// FCM Registration Error
PushNotifications.addListener("registrationError", (error: any) => {
});
// Notification received when the app is in foreground
PushNotifications.addListener("pushNotificationReceived", (notification: PushNotificationSchema) => {
});
// Notification data when the user tab the notification in system tray
PushNotifications.addListener("pushNotificationActionPerformed", (notification: ActionPerformed) => {
});
PushNotifications.register();
}
}, [ready])
return (
<BlaBlaBla />
)
}