awslabs / aws-mobile-appsync-sdk-ios

iOS SDK for AWS AppSync.
https://awslabs.github.io/aws-mobile-appsync-sdk-ios/
Other
262 stars 130 forks source link

how can I send a push notification (apns) when using appsync #8

Closed alionthego closed 11 months ago

alionthego commented 6 years ago

what is the best workflow to use in order to send push notifications to an iOS device when using appsync.

I have a chat app and want users to receive push notifications from apns when they are not using the app and a message is sent to them.

I have all the certificates and have successfully sent push notifications from the sns console. I don't know however how to integrate that with appsync.

My thoughts are to use a lambda function to send the notification directly when an appsync message is sent but could not find any examples on how to do that.

rohandubal commented 6 years ago

Hello @alionthego

Currently, the best solution would be to attach a resolver with None data source and connect to a lambda and use it to publish notifications using AWS Pinpoint.

Unfortunately, I do not have an example on how to do this, but I will discuss with the team if we can document this use-case. I will keep this thread updated with details.

Thanks, Rohan

alionthego commented 6 years ago

Thank you. That's the last piece I need to have my app up and running with an appsync backend.

fans3210 commented 6 years ago

Hi may I ask in this thread about push notification too?

I'm implementing a chat app using Appsync. How can I detect whether a user is connected to avoid sending a push notification to him if he's online?

fans3210 commented 6 years ago

Hi @rohandubal , sorry I don't quite understand what u mentioned about we can 'attach a resolver with None data source and connect to a lambda and use it to publish notifications using AWS Pinpoint'. But How to invoke a lambda function from a none datasource? I only know how to invoke lambda function by using a lambda datasource

bernhardt1 commented 5 years ago

@rohandubal Was an example ever created as you described?

btn52396 commented 5 years ago

Can we receive a basic tutorial asap? I'm at the end of my project and this is one of the last things to implement

bernhardt1 commented 5 years ago

@btn52396 I ended up starting a fresh amplify project and dropping my previous appsync project into it.

I outline my steps to add notifications at the bottom of this comment: https://github.com/aws-amplify/amplify-js/issues/1415#issuecomment-447125676

Android notifications still aren't working so if you run into similar issues and figure it out can you let me know? Also, If you find a more comprehensive example could you link it?

palpatim commented 5 years ago

Sending this to @nikhil-dabhade to see if this is something we should add to the tutorial docs.

btn52396 commented 5 years ago

It would be greatly helpful as I'm actively waiting on a tutorial as I'm not sure what the proper way to do this and need to have this feature before I launch

palpatim commented 5 years ago

Recategorizing this as a documentation feature request for @nikhil-dabhade.

rohit3d2003 commented 5 years ago

Any guidance on this thread? I am also in the same boat as @alionthego and would appreciate guidance from this group.

ioschetan commented 5 years ago

Can we use push sync along with app sync to send the push notification?

bernhardt1 commented 5 years ago

@rohit3d2003 @btn52396

The best approach I've found involves using Amplify. Amplify encompasses Appsync + ties in a lot of other helpful utilities and services. You'll want to go through the Amplify setup to create an app with analytics & notifications. I also have Auth and Storage being handled by Amplify. https://aws-amplify.github.io/docs/js/start?platform=react-native

If you choose to use Amplify, then here is the path I took for setting up automatic push notifications:

  1. Update the your pinpoint endpoint so you can target based on userId. We can update the analytics endpoint when a user logs in with the new token & their user id. This will allow us to follow them from device to device with notifications.

    Analytics.updateEndpoint({
        address: notificationToken, // The unique identifier for the recipient. use device token
        optOut: 'NONE',
        userId: userId
      })
  2. try to send a notification using the users id: https://stackoverflow.com/questions/55188010/send-push-notifications-via-aws-pinpoint-to-specified-user

  3. Create a lambda function to trigger pinpoint notifications https://stackoverflow.com/questions/45274726/send-a-notification-by-lambda-function-with-aws-pinpoint

  4. Now you can use a lambda resolver in appsync or a dynamodb stream to trigger the lambda function and send notifications. I used both.

Let me know if anything is unclear.

rohit3d2003 commented 5 years ago

@bernhardt1 - This is exactly what I did. Didn’t realize to update this thread with it but thanks for documenting it

Swaroop-Bhupathiraju commented 5 years ago

Hi, is it possible to use push notifications only when user's app is in the background or inactive. Do not want to take a dependency on lambda and pinpoint when app is active (foreground)

bernhardt1 commented 5 years ago

@Swaroop-Bhupathiraju

Yes, it is definitely possible but it isn't plug and play.

You would need to keep track of when your users are active. You should find some tutorials or guidance on how to do this if you Google it.

If you have an accurate record of user activity then you can check against it before sending out push notifications.

Swaroop-Bhupathiraju commented 5 years ago

@bernhardt1 Thank you. I have the code to identify when app goes in the foreground and background. I have a chat app that works with appsync subscription. Assuming i store the foreground/background information in DynamoDB, i am trying to see if if i can continue to use appsync subscription (MQTT - without lambda and push notifications) when value is 'foreground' and lambda+push notifications only when value is 'not foreground' (may be using pipeline resolvers). Please let me know if this is doable and if there are any examples, i tried looking online

bernhardt1 commented 5 years ago

@Swaroop-Bhupathiraju

I think your idea for how to implement it is correct. I also have a chat component in my application and use an Appsync subscription to see new messages. I was not able to find any examples when I looked a few months ago.

I backlogged the task to do what you are trying to do. I don't mind letting the system send out a push notification to a user who is currently using the app. If you weigh the pros and cons it is not clear which approach is better.

  1. Users are offline 99% of the time so it is usually the right decision to send a push notification
  2. It will cost resources to run some logical check on dynamodb to know if a user is online or offline
  3. It will cost developer time to implement the user activity check
  4. it doesn't effect the user experience to send them a push when they are online

Maybe it is different for your situation but for the amount of work it wasn't worth it for me at this point.

Swaroop-Bhupathiraju commented 5 years ago

Thanks @bernhardt1

daghanacay commented 4 years ago

@bernhardt1

Can you tell us how you implement step 4 with lambda resolver?

Question for others; Also is there an example where I can subscribe to appsync from lambda? This is not the lambda resolver. Sooner sort of persistent lambda with express that listens to appsync subscription.

bernhardt1 commented 4 years ago

@daghanacay my resolver is simple

request

#**
The value of 'payload' after the template has been evaluated
will be passed as the event to AWS Lambda.
*#
{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": $util.toJson($context.source)
}

result

#**
Could return 0 to indicate the notification failed here
*#
$util.toJson(1)

I created a type in my schema with an extra field to hold the notification resolver which calls lambda. something like this:

type ChainNotification { id: ID ... notification: Int }

then you attach the resolver to notification.

...

It's a bit strange to explain if you aren't familiar with chaining resolvers. In my case:

  1. mutation comes in to add a chat
  2. The Chat data type has ChainNotification as a field
  3. That ChainNotification field gets the context of the mutation when you attach a resolver to it.

Hope this helps.

daghanacay commented 4 years ago

Thanks, I will look into it. New be to appsync 😄

Get Outlook for Androidhttps://aka.ms/ghei36


From: Jason Bernhardt notifications@github.com Sent: Wednesday, January 22, 2020 9:36:32 AM To: awslabs/aws-mobile-appsync-sdk-ios aws-mobile-appsync-sdk-ios@noreply.github.com Cc: daghanacay daghanacay@gmail.com; Mention mention@noreply.github.com Subject: Re: [awslabs/aws-mobile-appsync-sdk-ios] how can I send a push notification (apns) when using appsync (#8)

@daghanacayhttps://github.com/daghanacay my resolver is simple

request

**

The value of 'payload' after the template has been evaluated will be passed as the event to AWS Lambda. *# { "version" : "2017-02-28", "operation": "Invoke", "payload": $util.toJson($context.source) }

result

**

Could return 0 to indicate the notification failed here *# $util.toJson(1)

I created a type in my schema with an extra field to hold the notification resolver which calls lambda. something like this:

type ChainNotification { id: ID ... notification: Int }

then you attach the resolver to notification.

...

It's a bit strange to explain if you aren't familiar with chaining resolvers. In my case:

  1. mutation comes in to add a chat
  2. The Chat data type has ChainNotification as a field
  3. That ChainNotification field gets the context of the mutation when you attach a resolver to it.

Hope this helps.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/awslabs/aws-mobile-appsync-sdk-ios/issues/8?email_source=notifications&email_token=ABV2FLQAV676K5FEUNNOWT3Q652HBA5CNFSM4EVEEPGKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJRRHLI#issuecomment-576918445, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABV2FLSMSKFGBBNROUUVOATQ652HBANCNFSM4EVEEPGA.

atierian commented 11 months ago

Thank you for opening this issue. AWS AppSync SDK for iOS entered maintenance mode in September 2023 and will receive no further updates as of September 2024.

Please use Amplify Swift going forward. For information on upgrading to Amplify Swift, refer to the Upgrade from AppSync SDK documentation.