Push notification using FCM for web app in Ruby on rails - ruby-on-rails

I have previously worked on push notifications with FCM for android app. From Firebase console i got server key to which my backend program hits when the notification were expected to be delivered. Also I could easily get the device token from the console(on client side) to which the server would send the push messages.
Now in similar way i would like to implement web push notification using FCM. Unfortunately, could not find FCM set up and implementation documentation using ruby. Besides bit confused as to how i can get the device token for web client to whom i would like to send push message/notification. What things do i need to look at and that i am missing?
I have come across web push gem and terms like VAPID keys and service worker but still confused how they work with FCM. If anyone could help me out with what i should be looking at and what i am missing. Thanks for reading this far.

Getting the FCM token for a device is only supported in client-side code. For web applications, see the documentation on getting the registration token. You can then pass the token from the client to Ruby backend, where you can store and use it to send a notification to that specific client.
To send a message to the token from your Ruby code, you can call the FCM REST API, after providing proof that your request is authorized with one of the methods outlined in the docs on authorizing send requests.
If you're having trouble making this work for your use-case, it's more likely that someone can help if you edit your question to show what you tried based on the above information and other existing resources.

Related

FCM/APNS integration works fine for new tokens, but not when converting old APNS tokens?

I have an FCM project sending push notifications to both iOS and Android users successfully. New iOS users subscribe and retrieve an FCM token.
I however also have a number of users on old iOS apps with direct APNS subscriptions and I tried converting one old APNS token to an FCM token and send a message using this via FCM.
I used the API described here: https://www.thepolyglotdeveloper.com/2017/06/apns-tokens-fcm-tokens-simple-http/
Sending a message using FCM to the converted token, I get the error: Auth error from APNS or Web Push Service. A stackoverflow article on this error suspects project setup issues, but can this even be the case if iOS users do get pushes with new FCM tokens?
Any hints on where this goes wrong and what else I could try out would be appreciated, I am very much in the dark here.
Found it myself eventually. I had used the wrong value for the "application" parameter in the conversion API call.

Sending Notifications to user while app is in background

I've done lots of research on this from Firebase to App delegate but I don't understand a lot. I'm trying to send notifications to the user when someone else has posted something in my app - like on Instagram or social media post notifications. Can someone please point me in the right direction for how to do this? Thanks a lot in advance!
Overview
If you have setup your iOS app for Firebase Cloud Messaging (FCM), when the user first opens your app, the apps talks to the google servers and generated a token specific to that device.
What is a token?
The token is like a device identifier. You use that token to tell firebase which exact device you want it to send the notification to. (The token changes on uninstalling and reinstalling the app and may even on a restore).
On the backend
Now, you have to configure your app to send that token to your backend and store it as one of the tokens for the user currently logged in. (This should happen every time they login from that device, and you should remove that token from the server when the user logs out).
Whenever you need to send out a notification, you'll have to make a POST request to the FCM server. This POST request will contain the device token and the content of the notification, along with other authorization info. Detailed instructions are provided at
Send a Notification Message

Using Amazon SNS with React Native

Can anyone provide a simple example of using the Amazon SNS service to send and receive Push Notifications under React Native?
I've got the Amazon SNS service setup, and I can publish a message from the website and have it appear on an iPhone – if I manually enter the device token and initiate the publish on the Amazon website. Now I want to automate the process of registering the Push Token in Amazon SNS, and allow the app to Publish a push to the SNS service.
I know that Amazon has a very new project to integrate their SDK into React Native, but I cannot get it working. Support on that repo is spare too, so I'm hoping someone here can help.
Right now what I get is: Undefined is not an object (evaluating 'cognateClient.initWithOptions')
The GitHub issues mention something similar on Android, but obviously that doesn't help with my iOS app.
What I'm looking for is a step-by-step breakdown of how to integrate Amazon's code in to an existing RN app. They don't provide a standard Node package, so I'm struggling to figure out how to get the React Native code and the SDK to play nice with Xcode.
Any suggestions? Thanks!
There's no 'integration' of AWS into the RN app needed. You just need the device token from RN.
Use a package like RNPN (https://github.com/zo0r/react-native-push-notification) in order to get the device token your phone generates. When you have that, make a call to your api, and your api is then responsible to call out to SNS to register that device token as an SNS 'endpoint'.
Translate this pseudo-code into whatever your API is written in: https://docs.aws.amazon.com/sns/latest/dg/mobile-platform-endpoint.html
In short...
RNPN onRegister ->
POST /registerDevice on your api ->
call AWS
createPlatformEndpoint from your api ->
start sending messages from your api
Most likely you'll want to store some relationship from user -> deviceToken+platformEndpoint in your db/data store.
Then you can send notifications from your api. Wrote up how to do that here: https://medium.com/#duhseekoh/send-a-push-notification-using-aws-sns-javascript-sdk-2f117e9c4180
Im using react-native-fcm with Amazon sns, it will register my device token and then we are sending that token to our php solution that then add the device to a subscription on a topic in SNS, passed back to the app is the subscription token and endsub tokens. Then we Push Messages to the topic thru SNS. Works on both iOS and Android.

APNS with Firebase version 3

I've set up Firebase to send push notifications to users of my app. However, I'm not sure what's best practice regarding user-to-user communication, such as chat clients. Should I get the device token at each startup of the app and use that for sending, or should I create a new topic for each "chatroom" that both parties are required to subscribe to? For instance when accepting a chat request.
If the first option is the best, how does this work? If the device is assigned a new token upon app startup, how can I be sure a given ID points to a specific device? The whole concept seems fragile - but could someone guide me to the most efficient solution?
I'm only looking to send chat messages / chat invites with push
Depending on the size and the privacy of the chatroom you can choose between using:
topics made for big groups, and without protections on joining / leaving
device tokens that you need to store in your server implementation.
On the plus side they allow you to control the who is receiving the
messages, and to send messages to individual device.
The device token does not change every startup.
It is created when the application is launched for the first time, and can be updated in special cases. When the token change (again, this is rare) the FirebaseInstanceIdService.onTokenRefresh() callback is called.
In a generic chat application you might want to:
first authenticate the user with your login system
upload to your server the mapping user-id > device-token
send messages to the users via the FCM server-side APIs.
Update to address one of the comments:
The server-side API allows to send the same message to multiple tokens in the same HTTP request. See registration_ids in https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
You should not use the server-side API in the client, because that would require you to add the API-KEY in the application which is a security issue (people could decompile the app and read the key)
The storage structure is up to you. For user-id > multiple-device-tokens a dictionary could work.
If the token are non reusable. So it's safe to send messages to expired token.
Token generation requires internet connectivity, so it could take some time.
To handle token after user registration see this question:
How to launch FCM ID Service only after a particular activity is triggered?

Mailcore2: how to receive mail instantly when a new mail arrived?

I'm building an iOS mail client for receiving mails via mailcore2, with IMAP protocol, I've fetched the mail messages successfully. But, how to implement the remote notification push when the target mail account received a new mail in client? I checked the mailcore2 project and some google search results, no luck there.
Please help, thanks!
UPDATE:
Let me clarify the problem details, for the mail client like Airmail in Mac and Microsoft Outlook in iOS, they support to add a IMAP protocol mail account for sending and receiving e-mails, e.g. I added my qq.com mail to Outlook for iOS client, once a new mail arrived to my INBOX in qq mail, I could get an instant remote notification from Outlook client right now.
Let me guess, the mail client didn't upload/save my IMAP-protocol mail config to microsoft/outlook server, all the receiving mail logics just work in local, including communication protocol.
How about IMAP? I've check this article on difference between POP and IMAP. IMAP just keep the client and server have the same account mail data all the time.
Airmail in Mac used mailcore2 in its implementation, so how does it make sure the remote notification work? I don't think they deployed a server side to check the new email arrival for all the user accounts, really.
On mailcore2, I think this is the key point, the remote push should be implemented by it, not some unknown servers. I checked the project structure basically, it's so huge for me and there are many c++ code logics, I must read many mail related blogs before reading it. If someone knows the related guide/wiki/header files, please share to me, or point the mistake on my above guess out, great thanks!

Resources