Simple Machines Community Forum

Customizing SMF => Modifications and Packages => Topic started by: vbgamer45 on February 01, 2019, 05:29:54 AM

Title: Push Notifications
Post by: vbgamer45 on February 01, 2019, 05:29:54 AM
Link to the mod (https://custom.simplemachines.org/index.php?mod=4202)

For SMF 2.0.x and SMF 1.1.x

Adds push notifications for Chrome and Firefox using the OneSignal api.
Works for replies and adds support for the following mods
Awesome Post Ratings
@Mentions Mod
Who Quoted Me

Requires an OneSignal account and cURl support for PHP


Install Information:
Install via the SMF's Package Manager via upload package.


############################################
Title: Re: Push Notifications
Post by: pocttopus on February 01, 2019, 06:56:33 AM
Interesting feature... these notifications were missing for SMF.
But where is Who Quoted Me mod?  ???
Title: Re: Push Notifications
Post by: Mick. on February 01, 2019, 07:21:47 AM
Bro, very cool. I was just thinking about this the other day as I noticed a few websites prompted me with a small popup if I wanted to be notified. Interesting, Im going to try this and see whats up  ;)
Title: Re: Push Notifications
Post by: Arantor on February 01, 2019, 07:40:43 AM
Quote from: pocttopus on February 01, 2019, 06:56:33 AM
Interesting feature... these notifications were missing for SMF.
But where is Who Quoted Me mod?  ???

SMF 2.1 can do this but not with push notifications. Though I love how your first reaction is not "oh, a cool new thing" but essentially a criticism.
Title: Re: Push Notifications
Post by: Shambles on February 01, 2019, 07:46:24 AM
Quote from: pocttopus on February 01, 2019, 06:56:33 AM
But where is Who Quoted Me mod?  ???

It's an SMFHACKS mod:    https://www.smfhacks.com/index.php?topic=9724.0
Title: Re: Push Notifications
Post by: pocttopus on February 01, 2019, 08:35:10 AM
Quote from: Arantor on February 01, 2019, 07:40:43 AM
Quote from: pocttopus on February 01, 2019, 06:56:33 AM
Interesting feature... these notifications were missing for SMF.
But where is Who Quoted Me mod?  ???

SMF 2.1 can do this but not with push notifications. Though I love how your first reaction is not "oh, a cool new thing" but essentially a criticism.
I had tried to use 2.1 but only on localhost and that's not the real feeling how 2.1 behaves comparing to 2.0.15 which is our current version on our community, so I did not noticed that quote notifications are implemented on 2.1. :-\
I had no intention to write criticism, I was just curious about the mods which are supported by "push notifications".  :)

Quote from: Shambles on February 01, 2019, 07:46:24 AM
Quote from: pocttopus on February 01, 2019, 06:56:33 AM
But where is Who Quoted Me mod?  ???

It's an SMFHACKS mod:    https://www.smfhacks.com/index.php?topic=9724.0
Thanks!  ;)
Title: Re: Push Notifications
Post by: -Rock Lee- on February 01, 2019, 09:48:23 AM
Great, that's what I was looking for. Now to test it on a test site and see if it's what I need... Thank you for your work @vbgamer45!


Regards!
Title: Re: Push Notifications
Post by: vbgamer45 on February 01, 2019, 09:57:39 AM
The who quoted mod is in the approval process as well.

I originally did the push notifications system for create a forum but then realized the service I used did not support wildcard dns subdomains without having it setup for each.

I can add some more notifications and toggles to turn then on and off depending on feedback.
Title: Re: Push Notifications
Post by: Mick. on February 01, 2019, 11:39:20 PM
Hey bro, whats' the difference between your mod and this tiny script you can add into the index.template in the <head> section taken from their website?  :o

<link rel="manifest" href="/manifest.json" />
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
<script>
  var OneSignal = window.OneSignal || [];
  OneSignal.push(function() {
    OneSignal.init({
      appId: "**********************************",
    });
  });
</script>
Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 08:48:55 AM
That is a base script. That script doesn't track members. You can't do mass messages though their backend.
Doesn't have subscribe,unsubscribe options via html.  And you wouldn't be able to receive alerts if someone replied to your topic.
Title: Re: Push Notifications
Post by: Mick. on February 02, 2019, 08:54:22 AM
Quote from: vbgamer45 on February 02, 2019, 08:48:55 AM
That is a base script. That script doesn't track members. You can't do mass messages though their backend.
Doesn't have subscribe,unsubscribe options via html.  And you wouldn't be able to receive alerts if someone replied to your topic.
Oh right on. Even better, I'm giving it a whirl
Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 09:08:58 AM
Have some fun with it maybe you can make it look nicer. I am backend developer so I just try to build out the core functions.
Title: Re: Push Notifications
Post by: Shambles on February 02, 2019, 10:05:53 AM
Hmm might have a play with this mod - thanks for making it available.

I manually installed the code from OneSignal and it seems quite accurate with its stats, plus the instant messaging is decent.
Title: Re: Push Notifications
Post by: Shambles on February 02, 2019, 10:56:58 AM
The mod installs the .js files into $boarddir but the code is looking in the website root for them:

QuoteFailed to register/update a ServiceWorker for scope 'https://www.i30ownersclub.com/': Load failed with status 404 for script 'https://www.i30ownersclub.com/OneSignalSDKWorker.js?appId=5212**********************202d'

$boarddir for me is https://www.i30ownersclub.com/forum/

Guess I'll just move the files up a level...
Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 11:09:37 AM
Yeah forgot about people in subfolders.
Title: Re: Push Notifications
Post by: Shambles on February 02, 2019, 11:13:31 AM
I've moved the three files up to the root but not a great deal is happening (no further errors in the console though).

Question:

Both the .js files contain the same importScripts command. Is this by design?

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');
Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 11:18:34 AM
I believe so
Title: Re: Push Notifications
Post by: Shambles on February 02, 2019, 11:44:27 AM
Just trying to work out why nothing happens when the mod is installed (ie, messages aren't received, notifications not requested).

Does the mod not require a manifest.json file?
Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 11:46:03 AM
Requires manifest.json

cURL needs to be installed.

After you click the subscribe to notifications in your user account and some replies. You should get a push notification in your browser. Tested on firefox and chrome.
Title: Re: Push Notifications
Post by: Shambles on February 02, 2019, 11:53:07 AM
I can see the manifest is built by the mod, just wondered if it needed <link rel="manifest" href="/manifest.json" />

When I inserted the OneSignal code manually in my index.template.php the whole thing worked perfectly.

Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 12:06:59 PM
I would include that as well just in case.
For the onesignal code it should be inserted automatically by the mod. And the code the mod uses passes the member id without it you will not get notified.
Title: Re: Push Notifications
Post by: Shambles on February 02, 2019, 12:41:22 PM
Should this class be declared somewhere?

<li><div class="onesignal-customlink-container"></div></li>
Title: Re: Push Notifications
Post by: vbgamer45 on February 02, 2019, 12:46:11 PM
You can check how I ran it on https://demoforum.createaforum.com/
Title: Re: Push Notifications
Post by: Mick. on February 03, 2019, 01:34:59 PM
Quote from: vbgamer45 on February 02, 2019, 09:08:58 AM
Have some fun with it maybe you can make it look nicer. I am backend developer so I just try to build out the core functions.
There's nothing to style! Hahaha other than that default red unsubscribe button but I think the user can do that directly from the mod's site.

ps,....I know I've been styling your stuff lol, I take no credit for your work. I just hack it. lol
Title: Re: Push Notifications
Post by: Mick. on February 04, 2019, 07:49:28 PM
Just realized that this is a browser basis thing. I logged in with diff users and it showed i was already subscribed. No big deal, That works for me.
Title: Re: Push Notifications
Post by: vbgamer45 on February 04, 2019, 07:58:29 PM
Hmm will need to look into that.
Title: Re: Push Notifications
Post by: Mick. on February 04, 2019, 08:02:38 PM
Quote from: vbgamer45 on February 04, 2019, 07:58:29 PM
Hmm will need to look into that.
There's a good chance this is all on OneSignal
Title: Re: Push Notifications
Post by: Biology Forums on February 05, 2019, 01:47:56 AM
Ahmmm... 1.x?
Title: Re: Push Notifications
Post by: Arantor on February 05, 2019, 02:45:39 AM
Quote from: Study Force on February 05, 2019, 01:47:56 AM
Ahmmm... 1.x?

You know 1.1 is in all practical senses EOL, right?
Title: Re: Push Notifications
Post by: Biology Forums on February 05, 2019, 03:00:44 PM
Thanks for the reminder, looking forward to the next
Title: Re: Push Notifications
Post by: addlife on February 10, 2019, 05:36:29 PM
Quote from: vbgamer45 on February 01, 2019, 05:29:54 AM
Link to Mod (https://custom.simplemachines.org/mods/index.php?mod=4202)

For SMF 2.0.x

Adds push notifications for Chrome and Firefox using the OneSignal api.
Works for replies and adds support for the following mods
Awesome Post Ratings
@Mentions Mod
Who Quoted Me

Requires an OneSignal account and cURl support for PHP


Install Information:
Install via the SMF's Package Manager via upload package.


############################################

Hi thanks for this mod. I don't really know how to code.

I installed the mod. Signed up for a onesignal account. I think I set up the webpush thing on onesignals website correctly. Just a FYI my test website isn't https, I don't know if that makes a difference.

I'm holding by the part where it tells me to insert some code into the <head> of my website. Which file would that be? I'm assuming it's one of the files in the public_html folder on my cpanel file manager (maybe index.html_ or index.php), is that correct?

Also how do I know if my website supports that cURl thing mentioned?

Any help would be appreciated.
Thanks.

One more thing is it possible to only have notifications sent for mentions and quotes, but not for replies?
Title: Re: Push Notifications
Post by: Mick. on February 10, 2019, 05:57:01 PM
When you install the mod, everything its already installed. No need to add any codes from Onesignal. Just sign up, get your numbers.
Title: Re: Push Notifications
Post by: vbgamer45 on February 10, 2019, 07:26:07 PM

Quote
One more thing is it possible to only have notifications sent for mentions and quotes, but not for replies?
Not at the moment but would be possible to do.
Title: Re: Push Notifications
Post by: addlife on February 10, 2019, 07:45:04 PM
Quote from: Mick. on February 10, 2019, 05:57:01 PM
When you install the mod, everything its already installed. No need to add any codes from Onesignal. Just sign up, get your numbers.
Thanks. I see it now in the default theme. At first I didn't see the button. Sometimes it's there and sometimes not.

I tested it out and it worked so apparently I have the curl thing.

Quote from: vbgamer45 on February 10, 2019, 07:26:07 PM

Quote
One more thing is it possible to only have notifications sent for mentions and quotes, but not for replies?
Not at the moment but would be possible to do.
Ok. thanks for responding.

Also it seems like that for any post that's made anywhere on the forum it sends a notification. Is that the way it's supposed to be?
Title: Re: Push Notifications
Post by: vbgamer45 on February 10, 2019, 09:11:31 PM
It should be any reply made to your post.
Title: Re: Push Notifications
Post by: Mick. on February 15, 2019, 07:31:42 AM
Alright bro, It seems that notifications are for desktop browser only. All this time I thought i'd get notifications on my mobile but didnt. I ended up creating an app on Firebase using One Signal docs and now I get notified on my Android phone. It only took 3 minutes to setup. Thought you should now.
Title: Re: Push Notifications
Post by: vbgamer45 on February 15, 2019, 08:09:23 AM
Is that another code you had to the website?
Title: Re: Push Notifications
Post by: Mick. on February 15, 2019, 08:33:26 AM
Quote from: vbgamer45 on February 15, 2019, 08:09:23 AM
Is that another code you had to the website?
Nope. No need. Just direct the mod users to generate a Firebase server key and add it to the mobile section in OneSignal settings.

In OneSignal App settings, we have the browser settings, like for Chrome and other browsers minus Safari. Below it You'll see Native App Platforms, click on Google Android and will ask for Firebase Server key.

Follow this doc https://documentation.onesignal.com/docs/generate-a-google-server-api-key very simple to setup.
Title: Re: Push Notifications
Post by: Mick. on February 15, 2019, 08:37:58 AM
I would think this can be added to admin settings as well but for iOS, well, I looked at it and that seems a whole 'nother ball game especially for users that have no knowledge
Title: Re: Push Notifications
Post by: Mick. on February 15, 2019, 10:26:24 AM
Also, I just noticed that you have to be logged on to the forum to receive notifications. At least on mobile. I'll check on browser later today.
Title: Re: Push Notifications
Post by: vbgamer45 on February 15, 2019, 10:30:49 AM
Yeah it is based on member id thats how notifications gets sent to right person. I add member id as a filter from onesignal.
Title: Re: Push Notifications
Post by: addlife on April 16, 2019, 04:37:53 AM
Is it possible to have the button only displayed on a certain page? For example I would create a thread titled "Push Notifications" then on that page the button will show up.
Title: Re: Push Notifications
Post by: vbgamer45 on April 16, 2019, 08:23:18 AM
yes but you would have to change the logic for it.
Title: Re: Push Notifications
Post by: refx on May 13, 2019, 02:01:59 PM
Quote from: vbgamer45 on February 15, 2019, 10:30:49 AM
Yeah it is based on member id thats how notifications gets sent to right person. I add member id as a filter from onesignal.

Hello,
I've installed the plugin, after copying .js files to root, it started working (before that I got 404 errors on onesignal panel). But I got many user reports who are unable to subscribe. When I check oneSignal users/allusers page, under tags column,  While I get couple of user id and uname correct, I see that there are multiple entries tagged "{uid: 0}" with different player ids. Also most of the tags are empty, totally blank tag information. App Version column is also completely blank.

Any ideas what's wrong with my installation?
Title: Re: Push Notifications
Post by: Christopher Scott Case on August 09, 2019, 12:27:38 PM
@vbgamer45 Hey, I've noticed that on replies to forum messages, I get notified when I reply to a topic I started, not just other people. It's annoying enough both myself and several of my users have decided not to use the notifications, because it's frustrating to get that extra notification.

If you can point me in the right direction, I'll gladly make the modification to filter out self replies myself. I was looking at the code, and I think I see where you're setting up a filter by user id, but as a JS dev with little PHP knowledge (and no SMF modding knowledge), I might not be following it correctly.
Title: Re: Push Notifications
Post by: Biology Forums on August 09, 2019, 02:08:09 PM
Quote from: Christopher Scott Case on August 09, 2019, 12:27:38 PM
@vbgamer45 Hey, I've noticed that on replies to forum messages, I get notified when I reply to a topic I started, not just other people. It's annoying enough both myself and several of my users have decided not to use the notifications, because it's frustrating to get that extra notification.

If you can point me in the right direction, I'll gladly make the modification to filter out self replies myself. I was looking at the code, and I think I see where you're setting up a filter by user id, but as a JS dev with little PHP knowledge (and no SMF modding knowledge), I might not be following it correctly.

Let me get this straight, you start a topic, you reply to your topic, then you get notified (that's odd)
However, when others reply to your topic, you do not get notified (another oddity). Am I reading this correctly?
Title: Re: Push Notifications
Post by: grzeg8102 on September 17, 2019, 10:19:53 AM
Hi vbgamer45!
First of all - great mod! SMF lacks such basic and common functionalities as web push, so this mod is really useful.

I wanted to report some bugs to you. They relate to the @mentions mod. There are a couple of issues - all related to the @mentions mod you provide support to:
1. mod installation file contains an error.
File 'webpush2.xml' contans:
<file name="$languagedir/Mentions.php" error="skip">
should contain:
<file name="$sourcedir/Mentions.php" error="skip">
This mistake basically adds no support for the mentions mod during installation of push notifications

2. There's a bug in the code to be modified inside Mentions.php resulting in a syntax error - HTTP 500 durin execution of the PHP code
File 'webpush2.xml' contans:
$pushurl = $scripturl . '?msg=" . $id_post];
should contain:
$pushurl = $scripturl . '?msg=' . $id_post];

3. and the last bug in the code to be modified inside Mentions.php. There are no title nor message body parameters passed in callup of function webpush_send. The push notification when someone mentions the user is empty
File 'webpush2.xml' contans:
webpush_send($mention['id'], $user_info['id'], $user_info['name'],'tag',$pushurl);
should contain:
webpush_send($mention['id'], $user_info['id'], $user_info['name'],'tag',$pushurl, array('message'=>$subject, 'title'=>''));
Title: Re: Push Notifications
Post by: vbgamer45 on September 17, 2019, 10:39:07 AM
Fixed and  updated! Thanks for the fixes

1.0.1 Fixed some bugs for mentions mod support thanks to grzeg8102
Title: Re: Push Notifications
Post by: pocttopus on September 20, 2019, 05:58:41 AM
Hello,

This updated modification renames public_html/Sources/Mentions.php file and forum is going down.
I had no other option and I had to remove it somehow and reinstall @mentions mod again.
Title: Re: Push Notifications
Post by: sam666 on September 20, 2019, 06:47:53 AM
Same issue with updated mod, breaks forum  :-X
Title: Re: Push Notifications
Post by: vbgamer45 on September 20, 2019, 08:35:47 AM
Quote from: pocttopus on September 20, 2019, 05:58:41 AM
Hello,

This updated modification renames public_html/Sources/Mentions.php file and forum is going down.
I had no other option and I had to remove it somehow and reinstall @mentions mod again.
What do you mean by renames? How does it change?
Title: Re: Push Notifications
Post by: sam666 on September 21, 2019, 03:54:09 AM
I get the following error after installing. Running PHP7.1.30 SMF 2.0.15
Parse error: syntax error, unexpected 'id' (T_STRING) in */Sources/Mentions.php on line 256

webpush_send($mention['id'],$user_info['id'],$user_info['name'],'tag',$pushurl, array('message'=>$subject, 'title'=>''));
Title: Re: Push Notifications
Post by: vbgamer45 on September 21, 2019, 09:36:03 AM
Posted a new update to fix this issue.
Title: Re: Push Notifications
Post by: sam666 on September 21, 2019, 10:10:31 AM
Installed with no errors. Thanks vbgamer45. Not had chance to test the bug fixes yet though.
Title: Re: Push Notifications
Post by: vbgamer45 on July 29, 2020, 04:16:06 PM
1.0.1a
+Added support for SMF 1.1.x
Title: Re: Push Notifications
Post by: grzeg8102 on August 04, 2020, 10:28:34 AM
Hi vbgamer,
I've spotted another small bug for push notifications. It bugged me for months why the website opened by push notifications shows the 1st page of a thread rather than navigating directly to the reply and today I found this.
<file name="$sourcedir/Subs-Post.php">

<operation>
<search position="replace"><![CDATA[trackStats(array('posts' => '+'));]]></search>
<add><![CDATA[trackStats(array('posts' => '+'));

// Push Notifications Mod SMFHacks.com
global $sourcedir, $scripturl;
require_once($sourcedir . '/webpush2.php');
$pushurl = $scripturl . '?topic=' . $topicOptions['id']. ".msg=" . $msgOptions['id'];

Last line contains a misformed parameter for the SMF forum, which the forum is unable to understand and opens the 1st page. The last line should look like this:

$pushurl = $scripturl . '?topic=' . $topicOptions['id']. ".msg" . $msgOptions['id'] . '.html#msg' . $msgOptions['id'];

So basically no `=` sign is necessary for the msg id. This makes the forum to navigate to the biginning of the right page with the last reply. However ideally we'd love to see the reply content from the push notification, right? And that's why we need to use the anchor in the link, so `#msgXXXX`.
After that the link is formatted correctly enough to be interpreted correctly bo SMF and opens the right URL with the reply.
Title: Re: Push Notifications
Post by: Arantor on August 04, 2020, 10:35:00 AM
Um. You're both kind of wrong :/

The correct line would actually be:
$pushurl = $scripturl . '?topic=' . $topicOptions['id']. ".msg" . $msgOptions['id'] . "#msg" . $msgOptions['id'];

Any use of '.html' in SMF URLs is only valid for queryless URLs which is not the default, not used throughout the rest of SMF and only implemented as a last-minute replacement in the buffer (i.e. almost none of SMF actually outputs .html URLs, it outputs the URLs of the format I outlined and is converted *only* if that option is turned on, last minute before sending the page to the browser.

And of no benefit to modern search engines.
Title: Re: Push Notifications
Post by: vbgamer45 on August 04, 2020, 10:39:15 AM
I think I had an issue sending the # which I why I took it I could be wrong though don't remember. Give it a try though and let me know.
Title: Re: Push Notifications
Post by: Arantor on August 04, 2020, 10:47:49 AM
Even if you have no # in it, you need to not have .msg= in the URL but .msg123 as that's what SMF picks up.
Title: Re: Push Notifications
Post by: vbgamer45 on August 04, 2020, 11:35:54 AM
Update
1.0.1b
!Minor url for forum posts fix thanks to Arantor.



Note didn't add the # in the url
Title: Re: Push Notifications
Post by: Arantor on August 04, 2020, 11:46:23 AM
Now that I think about it, if # is a problem in the notification itself, I actually wonder if it wouldn't be better linking directly to index.php?msg=123 (not to the topic but directly to the message), which would when opened be a redirect to the message complete with its # anchor to scroll to the correct place once the notification opens in whatever browser it's going to open in.
Title: Re: Push Notifications
Post by: vbgamer45 on August 04, 2020, 11:54:51 AM
Hmm, that might do the trick!
Title: Re: Push Notifications
Post by: aegersz on August 25, 2021, 05:42:46 PM
Hi, I use my phone as my main internet connection and the notifications go to the phone in all cases but sometimes they also come through to the tethered desktop about 10 minutes later - any ideas ?

I have cleared my cookies.