Categories
Uncategorized

Piggy Backing on ReactJS’s ServiceWorker for Notification Event Listeners

About a week ago I decided to start learning ReactJS because why not? just to get some exposure to yet another javascript framework that seems to be pretty popular right now. I had been in the middle of putting together a notification/reminder web app (more on that in a later post) and wasn’t particularly happy with the code I ended up putting together to populate the UI. Fast forward to yesterday, I gained some rudimentary knowledge on how to use React, converted my existing jquery based web app to React (along with my ServiceWorker code that was detecting when a notification was clicked or closed) and I was now ready to do a production build of the app. Upon doing the build and testing the resulting site, I found out that my service worker code was no longer firing.

After doing some digging I found out that React uses its own service worker to enable itself to run when your browser or device is offline. I was thrilled that I got that for free out of the box, but it also meant that my service worker was no longer running since you can only have 1 service worker running at a time for a single URL scope.

My solution for this was to merge my service worker code in with React’s service worker. Just a warning: This may not be recommended by React, and it might only work for my particular case because I just need to add some event listeners to the service worker. Your mileage may vary.

Since I was no longer able to run any code during the register phase of the service worker (since React is doing the registration), I needed to do the following in order to get a reference to the registration object whenever I want to call showNotification()



navigator.serviceWorker.ready.then(registration => {
    registration.showNotification("Notification Text",
    {
        data: {
            id: "recordid"
        }
    });
});

On the service worker side, I just need to paste in my event listeners into the service-worker.js file in the /build directory after building the React project. Keep in mind, you will need to do this every time you re-build your React project since that file will get replaced.



self.addEventListener("notificationclick", handleClickClose)
self.addEventListener("notificationclose", handleClickClose)

function handleClickClose(event) {
    const n = event.notification;
    sendMessage(n.data.id)
    n.close();
}

And thats all there is to it! You might notice that I’m calling a method sendMessage() in my event handler. I’m using this to let my application know which notification is being acted on. It isn’t required for the event handler to work, but it is part of the functionality for the web app I’m working on. sendMessage() is implemented as follows:



function sendMessage(pstrMessage)
{
    clients.matchAll({includeUncontrolled: true, type: 'window'}).then(clients => {
        clients.forEach(client => {
            client.postMessage(pstrMessage)
        })
    })
}

And I have an event listener that is registered during componentDidMount() in my React app’s root class that listens for messages from the service worker:


navigator.serviceWorker.addEventListener('message', function(event){
     // do some work here
     console.log("click notification for " + event.data)
});

Just one final note, I wrapped the registration of my original service worker in an if statement that checks if I’m in dev mode or not. This way it will still register during development (since the React service worker isn’t registered) and it is prevented from registering in production.



if (window.location.port === "3000")
{
     navigator.serviceWorker.register('/sw.js').then(...)
}

I imagine if I was better versed in React and building npm packages that there might be some way to bundle this up into a cleaner package that would make it more reusable and tie into the React build process in order to skip the manual step of copy/pasting the event listeners into the service-worker.js file. If anyone stumbles on this article and has knowledge of how to do that please let me know!

Categories
Uncategorized

New iOS App In The Works

My first foray into native (non Cordova) iOS apps has been in the works for the last couple of weeks. I read through Matt Neuburg’s iOS 10 Programming Fundamentals with Swift and Programming iOS 10 (although I cherry picked the parts I thought I’d need in this one) and then got started writing my first app, tentatively titled WallpaperCapture; A camera app that lets you take a photo for your iPhone lock screen and home screen with all of the labels and objects you’d normally see superimposed on the screen so you can frame the scene perfectly.

The goal with this app right now isn’t to get rich (I’m going to release it for free, possibly ad supported) but rather to practice what I’ve been learning, and experiment with interface objects and frameworks that I’ll likely need in the future on a much larger project. I’ve got a preliminary build that I’m testing right now that provides the time, date and other lock screen labels of an iPhone, along with the Perspective Zoom so that the photo that you take will look exactly as it does when you set it as a lock screen photo. The photos are currently stored into an app specific album in the Photos app, but I’m planning on having an in-app library to see all of the photos that have been taken. Eventually I will provide a way to upload the best images ‘to the cloud’ so that the photos can be shared with other users looking for interesting and unique wallpapers.

I’m in the process of implementing Fabric.io’s Crashlytics for app statistics prior to releasing the app to some users via TestFlight (email me if you are interested in being part of the beta test group). Once that is done and I’m ready to implement the ads and photo upload, I think I’m going to integrate with Firebase since it is an all in one SDK for ads, cloud storage and countless other features that I may need in the future.

Categories
Uncategorized

Experiment in SEO: CarKeyBatteryDepot.com

Over the last couple of weeks I have been getting up to speed with a few web development frameworks that I’ve been wanting to work on: NodeJS and bootstrap. I had experimented with both of them a little over the last few years, but I never had the chance to actually work on a project, until now.

I had the idea to put together a little website to search for car key replacement batteries when my Mazda 3’s car key was starting to get a little weak. Doing a google search returned some results but nothing definitive, other than a few youtube videos on how to replace the battery. Nothing outright stated that a 2014 Mazda 3 uses a CR2025 type coin battery, so it didn’t give me the confidence that I was actually getting the right battery without confirming it with the car manual. And further more, those youtube videos didn’t give me an option to buy an actual replacement battery.

So I set out to build a new site that would both definitively tell you what kind of battery your car key needs, and give you some cheap options to purchase a replacement battery and I called it CarKeyBatteryDepot.com. The website lets you specify your car’s year, make and model and key fob type (if there is more than one for your car) and it displays the type of replacement battery you need and provides you a list of vendors to purchase the battery from for cheap. The battery database was built by going through hundreds of car manuals (so far only Mazda and Toyota from 1999-2019 is supported) using a Python program I wrote to read the PDFs and extract the battery data. The purchasing options are pulled from Amazon’s Product Advertising API.

The site was built using NodeJS and NGINX with a LokiJS backend for fast searching and Bootstrap to provide a responsive UI. I followed Mozilla’s Beginner’s Guide to SEO to get the basics of SEO to give the site the best chance of being the top Google search result. Specifically, I’m using the following techniques:

  • ‘Meta Description’ tag in header, and all keywords in both the ‘title’ tag and at the top of the page
  • URLs for every possible battery response with the car’s year, make and model in the path. Ex: carkeybattery.com/2014/Mazda/3
  • sitemap.xml with a list of every possible URL so Google knows what to index
  • support SSL, with a free certificate obtained from LetsEncrypt.org

I’ve been waiting to see how well this works, but Google hasn’t indexed the site as of yet. This blog post is meant to both give me a way to introduce the site to anyone who is following my website, and to give Google another link to the site in the hopes that they consider this as me vouching for the site which should help its site rank. I’m hoping this little experiment works and that I can generate a few bucks off of Amazon referrals, as well as use it as an example when attempting SEO for other people. I’ll post a follow up once I have some definitive results.

Categories
Uncategorized

Programming Language Potpourri

At one point this morning I was thinking about what subject to tackle next and I started thinking about all of the different programming languages that I’m currently, actively, writing in at the moment between all of my contract and personal projects. It was getting a little scary when I realized there were about 6 different languages that I write in on an almost daily basis at my contract job, and another 2 that I kind of hack around with at home.

Contract #1

HTML5 Apps
– Javascript using the Sencha Touch and EXTJS framework
– Java for custom native Cordova plugins on Android
– Objective-C and Swift for custom native Cordova plugins on iOS

Main Web app and Webservices
– C# for latest main application and mobile REST services
– VB for previous main application still supported by the client
– Java for a custom web service for solving routing problems using jsprit
– TSQL for database stored procedures

Contract #2

50BookPledge.ca
– PHP for dynamic web pages
– Javacript for the usual cool site interaction stuff

84 North Studios Projects

SilverBeak.com
– Python using bottlepy for the site

And a bunch of other things using a combination of all of the languages already mentioned

That list ended up being bigger than I thought it was going to be when I first started writing this post. I had to go back and update the language count in the first paragraph a couple of times.

Listing all those out has started to worry me a bit about how well I actually know those languages. The doubt started creeping in yesterday when I stumbled on Standard JS Style and read through a couple of links. Mainly the part about not using semicolons to terminate a line. I knew they weren’t required, but I didn’t know why or in what situations you’d need to either make exceptions or handle the fact that the js interpreter might get confused (like starting a line with [ ( and some other chars). I’ve since done the requisite reading and now have the knowledge, but that just leads me to think what else don’t I know that I DON’T KNOW? I think I need to start trying to just buckle down and specialize in one or two of these areas that I use a lot and just learn all there is to know. I’m leaning towards javascript, and maybe even dumping PHP in favour of nodejs since I’ve been wanting to use it for a project lately but don’t know for what yet.

Categories
Uncategorized

Ok Seriously, I’m Back For REAL This Time…

So my last post was well over a month ago, not quite fulfilling the promise of having an active blog. But life has been busy since my fiancĂ©e and I bought a house. And now between trying to find ‘stuff’ to fill it with and getting all our paperwork in order, there just hasn’t been much free time to just sit and write.

So I’ve learned over the last month that adulting is hard, but I’m getting used to it and now I’ll have one more thing to write about here. Maybe this development-leaning blog will turn into a home improvement blog at some point?

Categories
Uncategorized

My ZazeenTV Experience

In an effort to reduce my cable tv bill to almost nothing, I decided to try out Zazeen TV’s IPTV service. They are offering their skinny basic tv package for $10/month (compared to $25/month the other cable companies are charging), and it included all of the ‘regular’ channels you’d expect to get, outside of sports and movies. The other benefit is with their basic package, you don’t have to also subscribe to their internet package, so I was able to continue using Rogers while I evaluated their service. It also meant that I could keep my free Rogers Gamecentre subscription so I wouldn’t lose out on watching hockey.

Once I subscribed, the set top box arrived in about 4 days. I plugged it in to my router and TV and powered it up. After a quick boot-up I was watching TV over my internet connection for only $10! I was pretty impressed with the technology until I started flipping between channels. Some looked and sounded great (like I was used to with Rogers) but others had visual artifacts and sounded like I was listening to a Skype call circa 2005 (like everyone was underwater).

Since most shows I’d want to watch were all streaming fine I thought I’d be able to live with it. I decided to try out the PVR functionality. It was as easy as plugging in a USB hard drive, rebooting the box and it was ready to start recording. I recorded a couple of shows, and it performed pretty much how you’d expect: recorded the show, played back normally.

In the end though, I ended up sending the box back and cancelling the service. The set top box itself was a bit flakey; it would occasionally just play static out of my speakers and would only stop if I rebooted it or it would reboot itself. This is a problem if you’re recording a show on another channel and your recording just stops because the box goes wonky. Also, the internet here can be a bit flakey as well, and while watching tv a couple of evenings it would just cut out and stop working, while my Rogers TV box kept working.

It’s those couple of things that make it seem like ZazeenTV just isn’t quite ready for heavy TV watchers. While reading through their user forums I came across a lot of posts where people referred to watching TV as something they do once in a while. So for the time being, I’m going to stick it out with over priced cable, and hope someone else starts up a reasonably priced live TV streaming service with higher quality service. Maybe something that works as an AppleTV app so I don’t need a separate box? That would be nice.

Categories
Uncategorized

Site Update: Instagram

I just published a new update to the site: Instagram posts. In-between regular posts you should now see my Instagram posts that correspond to the date range between the blog posts that they are physically between. I implemented horizontal scrolling if there are more than a page-width’s worth of photos (either swipe with your thumb on your phone or track pad, OR click on the arrow to scroll).

Categories
Uncategorized

Revival Of The Blog

Hi All!

It has been well over 3 years since my last post. I was doing alright with my 1 post a week rule up until the summer of 2012 when I guess I just gave up. It probably didn’t help that I started including tweets in my blog at some point. I felt like tweeting was a cheap and easy way to add content to the site, but it probably just made me lazy.

I decided recently that I needed to revive this thing, freshen up the look, and to give it some cool features to display what I can do and what I’m all about. So over the next little while, ontop of making regular posts, I’ll be adding some interactive features into this blog that you hopefully can’t find anywhere else, just to distinguish myself a little.

Other than the colour scheme change, and the jQuery type-along header, I’ve made some changes under the hood. The page was designed for mobile first, so if you inspect the source you’ll see some css media queries in there. I converted the MySQL database to MongoDB and it is hosted with mLab (formerly MongoLab). My website is hosted on a Linode virtual server (click that link to use my referral code should you decide to use them), and the website is written, as it always has been, in PHP. I’ll be re-working my editing tools in AngularJS, and I’ll probably do the same for the homepage at some point. I also implemented tagging and better permalink urls for individual blog posts based on the title (click a post title and then look at the URL to see what I mean).

Feature wise, I’m currently working on including my Instagram posts into the feed in some creative way. And I’ll be doing the same with my Twitter posts again as well.

This is probably a good start for now. I’ll do my best to keep on myself to regularly update the site, both with posts and features. I’m working on a lot of different projects at the moment, so I should have plenty of content to keep this place alive.

Categories
Uncategorized

403 Forbidden pt2: SymLinks Don’t Work After OS X 10.8 Upgrade

Continuing with my Apache woes from 3 months ago, I recently noticed a SymLink I had in my Sites directory was giving a 403 Forbidden error when I tried to browse into it.

Assuming it was yet another thing that got reset with the upgrade, I googled ‘apache follow symlink os x’ and stumbled across this great article Mac OS X, Web Sharing / Apache, and Symlinks.

I followed the first suggestion (adding FollowSymLinks to the username.conf file), restarted apache and I’m able to browse into the SymLink now.

Categories
Uncategorized

Does RIM *REALLY* Have A Shot At #3?

From BGR.com:

"We have a clear shot at being the No. 3 platform in the market. Carriers want other platforms. And we're not just another open platform running on another system. We're BlackBerry."

I’ve been seeing this quote around the web a lot today, and it got me thinking… does RIM really have a shot at #3?

My opinion is NO. Carriers may want other platforms, but at this point do any of the smartphone users?

And, yes, having the BlackBerry brand does carry some weight, but so does the Microsoft brand… and at this point, I think Microsoft has a way better chance with their Windows 8 phones than BlackBerry does with BB10.

After trying out BB10 on my Dev Alpha device, I don’t see anything extremely compelling about the OS, other than the fact that it’s NOT BB7.