OneSignal website push flow - web-push

I'm implementing OneSignal website push notification for my website. For now I just want to enable push notifications for the registered users. I had expected that there would be some callback methods after the users clicks "Allow" or "Block", but there aren't any. I see that I have to work with OneSignal.isPushNotificationsEnabled() and OneSignal.push(["getIdsAvailablegetIdsAvailable",..]), but I'm not sure how everything is supposed to come together.
What I think I should do is every time a registered users is at the homepage to execute something like this:
if isPushNotificationsEnabled
OneSignal.push(["getIdsAvailablegetIdsAvailable",..])`
send_user_id_to_server_where_it_is_saved_in_the_database
else
OneSignal.push(["registerForPushNotifications"..])
end
One thing that bothers me is that if this code gets executed and the user clicks "Allow" it will not trigger any code and a notification will never get to my server that the user has actually allowed push notifications. I'll have to wait for the user to come back or refresh the page. Is this how it's supposed to work or am I missing something?

After chatting with the OneSignal support, they made available a new method
OneSignal.on('subscriptionChange', function (isSubscribed) {..}]);
which will fire when the subscription status of the user changes. Note that a complete and valid subscription is based on a couple of things: notification permission, whether there is a background worker active to fetch notifications, whether the web database to store the user ID and registration token information is intact, whether the user has manually opted out or not.
There is a separate method that is not in the documentation, that is just for checking the user permissions:
OneSignal.push(["getNotificationPermission", function(permissions) {..}]);
The permissions parameter can be "default", "granted" and "denied".

Related

Retrieve Teams User ID based on AAD ID

I am trying to start a scheduled proactive conversation (the bot initiates the conversation on scheduled time).
I managed to get the User's AAD ID based on Graph API, but it doesn't match the Teams user ID. Tried for over 2 hours to obtain the right id, but I can't figure it out how. What would be the best approach I should take?
Have a look at the Microsoft Graph api to get the chat thread ID.
When the app is installed for the user, the bot will get receive a conversationUpdate event that will contain the necessary information for it to send the proactive message. For more information, see Bot events.
If you lose the chatThreadId, you can find it again by calling:
GET /users/{user-id}/chats?$filter=installedApps/any(a:a/teamsApp/id eq '{teamsAppid}')
However, this will only for for the personal scope! My advice would be to make sure you catch the conversationUpdate which is triggered after an install and persist the user details in a database.
I'm curious how you tried to "match" these? In any case, I don't think they're intended to match up in any way (the aadObjectId Guid and the "29:..." user id). As a result, you should store a mapping on your side (database or similar). You need to store ServiceUrl and ConversationId anyway to do proactive messaging, so just tack userid on as well.

Google Directory API users endpoint sometimes doesn't return orgUnitPath

I have setup watch channels for the User resource via the Google Directory API to receive push notifications when the resource changes or is created.
When my app receives a notification, it reads the e-mail of the user which was changed/created, then calls the users.get endpoint of the Directory API to get the latest data for that user and finally updates my database with that data.
In most cases this flow works fine, but I've observed that every now and then when a new user is created, the response from the users.get endpoint does not include the orgUnitPath property. It's not set to null, it's not even there. However, if I hit the endpoint again a while later, the property is there.
Does anyone know why this happens?

Web push notifications update VAPID keys

I previously used different push notification provider (they did not use VAPID keys) and got users subscribed to the service. Now, when I switched the push notification provider I'm in a situation where I kinda lost all previous subscribers, because they will not get the permission window again as they have already agreed to receive notifications. Is there a way to automatically update(re-subscribe?) the subscriber as he visits my webpage with a VAPID key pair so the "old" subscribers can still receive my push notifications?
Me again, I found that if you get the subscription with getSubscription() and then use the unsubscribe() function followed by another subscribe() function, you "update" the subscription and everything works without user interaction. The only real "issue" is that you can only update the subscription if the same user visits your page again.

Personalized web push notifications

I want to send web push notifications to registered users, are there any best practices on how to implement the cases when multiple users have access to the same device and one should not see the message of another user.
Thanks in advance.
A web push notification subscription is tied to the browser, not the device.
What you need to do is, map this id with your registered user when he logs in from a particular browser. Also, you need to remove the subscription id mapping with any other users in the system.
In the case of multiple users using the same browser, the above logic will make sure that at a time, a particular browser subscription id is linked only to a single user.
And when you want to send a notification to a registered user, you can retrieve all push subscription IDs linked to this user in your database, and trigger notifications to those subscription IDs.
And don't forget to unmap a subscription id when the user logs out from a browser. Otherwise, he will continue to receive all notifications even if he has logged out.

How to make your own push notification service for your website

I am working on a website, where I want to make a feature of notifications, when a user visits my website, they are asked for notifications permission and when they allow it, they will get notifications from my website, and whatever product I want them to get notified by.
Like for example when I visit some websites, they ask me for notifications permissions and when I allow the, I get notified through notifications then. That's all I want for now.
How can I achieve this functionality, I have follow this tutorial, but still confused how the users who allowed the notifications get detected and how all of them are notified then ?
Web Push library for node.js is just a sender.
You should obtain a subscription JSON object from the browser using Notification API and Service Worker API and then send it to your server, where put it in the database of your choise.
When you will need broadcast notifications, you can retrieve subscription and use a web-push library (for php is also available :)
Note that is a right flow looks following as:
1) Retrieve subscription from the browser
2) Send and store it on your web-server
3) Create notification prototype (just object)
4) Broadcast notification prototype ID you have created to the users
5) Service Worker receive one and fetch notification prototype from your server by ID
6) Show notification using browser API in service worker
For more see here links
https://developers.google.com/web/fundamentals/codelabs/push-notifications/
https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications/

Resources