2

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?

4

1 回答 1

3

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".

于 2016-03-23T17:05:49.900 回答