Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

May 29 2017

UK Tech Visas Quadruple After Applications Soar
Asus Goes Big On Slim Laptops at Computex
British Airways CEO Won't Resign, Says Outsourcing Not To Blame For IT Failure
US Might Ban Laptops On All Flights Into And Out of the Country

Get Started With Ionic Services: Push

In this tutorial, you're going to learn about Ionic Push, an Ionic service which makes it easy to send push notifications to your users.

Ionic Push allows you to send push notifications to the users of your app. These can be triggered whenever you choose. For example, when it's the user's birthday, you could automatically send them a push notification to greet them. 

How It Works

Ionic Push serves as a middle-man between the user's device and the Firebase Cloud Messaging. The first step is for the app to send its device token to the Ionic Push server. This device token serves as an ID which refers to that specific device. Once the server has that token, they can now make a request to the Firebase Cloud Messaging server to actually send a push notification to the device. On each Android device, a Google Play service is running, called the Google Cloud Messaging service. This enables the device to receive push notifications from the Firebase Cloud Messaging platform.

Here's a chart that shows the push notification flow:

Push notification flow

What You're Going to Build

You're going to build a simple app which can receive push notifications via Ionic Push. It will also use the Ionic Auth service to log users in. This allows us to try out targeted push notifications which will send the notifications to specific users only. The app will have two pages: the login page and the user page. Users should only be able to receive notifications when they are logged in.

To give you an idea how the notifications would look, here's a screenshot of the notification being received while the app is currently open:

in-app notification

On the other hand, here's what a notification looks like when the app is closed:

closed-app notification

Setting Up Push Notifications for Android

In this section, we will configure the Firebase and Ionic cloud services to allow push notifications. Push notifications in Android are mainly handled by the Firebase Cloud Messaging Service. Ionic Push is just a layer on top of this service that makes it easier to work with push notifications in Ionic apps. 

Create a Firebase App

The first step is to create a new Firebase project. You can do that by going to the Firebase Console and clicking on the Add project button. You'll see the following form:

Create Firebase Project

Enter the name of the project and click on the Create Project button.

Once the project is created, you will be redirected to the project dashboard. From there, click on the gear icon right beside the Overview tab and select Project Settings.

On the settings page, click on the Cloud Messaging tab. There you will find the Server Key and Sender ID. Take note of these as you will need them later on.

Cloud messaging settings

Create an Ionic App

Next, you need to create an Ionic app on the Ionic website. This allows you to work with the Ionic Push service and other Ionic services as well. If you don't already have an Ionic account, you can create one by signing up. Once you've created an account, you'll be redirected to the dashboard where you can create a new app. 

Create Ionic App

Create a Security Profile

Once your app is created, go to Settings > Certificates and click on the New Security Profile button. Enter a descriptive name for the Profile Name and set the Type to Development for now:

Create new security profile

Security Profiles serves as a way to securely store the Firebase Cloud Messaging credentials that you got earlier. Once it's created, it will be listed in a table. Click on the Edit button beside the newly created security profile. Then click on the Android tab. Paste the value for the Server Key that you got earlier from the Firebase console into the FCM Server Key field. Finally, click on Save to save the changes.

Add Firebase Cloud Messaging credentials

Bootstrapping a New Ionic App

Create a new Ionic 2 project using the blank template:

Once the project is created, install the phonegap-plugin-push plugin. Supply the Sender ID that you got from the Firebase console earlier:

Next, you need to install the Ionic Cloud plugin. This makes it easy to work with Ionic services inside the app:

Lastly, you need to update the Ionic config files so that Ionic knows that this specific project should be assigned to the Ionic app that you created earlier. You can do that by copying the app ID in your Ionic app's dashboard page. You can find the app ID right below the name of the app. Once you've copied it, open the .io-config.json and ionic.config.json files and paste the value for the app_id.

Building the App

Now you're ready to build the app. The first thing that you need to do is to fire up the Ionic development server so that you can immediately see the changes as you develop the app:

Once the compilation process is done, access the development URL on your browser.

Add Ionic App and Push Settings

Open the src/app/app.module.ts file and add the settings for the app (core) and push notifications (push). The app_id is the ID of the Ionic app that you created earlier. The sender_id is the sender ID that you got earlier from the Firebase console. Under the pluginConfig object, you can optionally set push notification settings. Below we're only setting the sound and vibrate settings to true to tell the hardware that it can play push notification sounds or vibrate if the device is on silent mode. If you want to know more about what configuration options are available, check out the docs on the Push Notification options for Android.

Next, let Ionic know that you want to use the cloudSettings:

Home Page

The default home page in the blank template will serve as the login page. Open the pages/home/home.html file and add the following:

To keep things simple, we only have a login button instead of a full-blown login form. This means that the credentials that we're going to use for logging in are embedded in the code itself.

Next, open the src/pages/home/home.ts file and add the following:

Breaking down the code above, first we import the controllers needed for working with navigation, loaders, and alerts:

Then import the services needed for working with Push and Auth.

Once those are added, import the User page. Comment it out for now as we haven't created that page yet. Don't forget to uncomment this later once the user page is ready.

In the constructor, check if the current user is authenticated. Immediately navigate to the user page if they are:

For the login function, show the loader and set it to automatically dismiss after 5 seconds. This way if something goes wrong with the authentication code, the user isn't left with an infinite loading animation:

After that, log the user in with the hard-coded credentials of a user that's already added in your app:

If you don't have an existing user yet, the Ionic dashboard doesn't really allow you to create new users, although you can create additional users once you already have at least one user. So the easiest way to create a new user is to call the signup() method from the Auth service. Just uncomment the login code above and replace it with the one below. Take note that you can create the user from the browser since the email/password authentication scheme just makes use of HTTP requests.

Now that you have a user that you can log in, you can go ahead and remove the signup code and uncomment the login code. 

Inside the success callback function for login, you need to call the register() method from the Push service. This crucial step enables the device to receive push notifications. It makes a request to the Ionic Push service to get a device token. As mentioned in the How It Works section earlier, this device token serves as a unique identifier for the device so that it can receive push notifications. 

The great thing about Ionic Push is its integration with Ionic Auth. The reason why we're registering the device tokens right after logging in is because of this integration. When you call the saveToken() method, it's smart enough to recognize that a user is currently logged in. So it automatically assigns this user to the device. This then allows you to specifically send a push notification to that user.

User Page

The user page is the page which receives push notifications. Create it with the Ionic generate command:

This will create the src/pages/user-page directory with three files in it. Open the user-page.html file and add the following:

To keep things simple, all we have is a button for logging the user out. The main purpose of this page is to receive and display push notifications only. The logout button is simply added because of the need to log the user out and test if they could still receive notifications after logging out. 

Next, open the user-page.ts file and add the following:

The code above is pretty self-explanatory, so I'll only go over the part which deals with notifications. The code below handles the notifications. It uses the subscribe() method to subscribe for any incoming or opened push notification. When I say "opened", it means the user has tapped on the notification in the notifications area. When this happens, the app is launched, and the callback function that you passed to the subscribe() method gets called. On the other hand, an incoming push notification happens when the app is currently opened. When a push notification is sent, this callback function also gets called. The only difference is that it no longer goes to the notification area.

For each notification, the argument passed to the callback function contains the object payload:

Push notification payload

In the above code, we're only using the title and the text to supply as the content for the alert. We're not limited to just alerts, though—as you can see from the screenshot above, there's this payload object which stores additional data that you want to pass in to each notification. You can actually use these data to direct what your app is going to do when it receives this kind of notification. In the example above, is_cat is set to 1, and we can have the app change its background into a cat picture if it receives this notification. Later on in the Sending Push Notifications section, you'll learn how to customize the payload for each notification.

Running the App on a Device

Now it's time to test out the app on a device. Go ahead and add the platform and build the app for that platform. Here we're using Android:

Copy the .apk file inside the platforms/android/build/outputs/apk folder to your device and install it.

Solving Build Errors

The first time I tried to run the build command, I got the following error:

Android build error

If you got the same error then follow along. If you haven't encountered any errors then you can proceed to the next section.

The problem here is that the SDK components mentioned were not installed, or there might be an important update that needs to be installed. However, the error message is a bit misleading, since it only says that the license agreement needs to be accepted.

So to solve the problem, launch the Android SDK installer and then check the Android Support Repository and Google Repository. After that, click on the Install button and agree to the license agreement to install the components.

Sending Push Notifications

Now that you've installed the app on your device, it's now time to actually send some push notifications. Here are a few scenarios which you can test out:

  • when a user isn't currently logged in
  • when a user is logged in
  • to all users
  • to users which match a specific query
  • when the app is opened
  • when the app is closed

The first step in sending a push notification is to go to your Ionic app dashboard and click on the Push tab. Since this is your first time using the service, you should see the following screen:

create your first push

Go ahead and click on the Create your first Push button. This will redirect you to the page for creating a push notification. Here you can enter the name of the campaign, title and text of the notification, and any additional data that you want to pass in. Here we're setting is_cat to 1.

push details

Next, you can optionally set the push notification options for iOS or Android. Since we're only going to send to Android devices, we only set the options for Android:

android push options

The next step is to select the users who will receive the notification. Here you can select All Users if you want to send the notification to all the devices which are registered for push notifications. 

Select users to push to

If you only want to send to specific users then you can also filter to them:

push to selected users

Take note that the users list is populated from users that are registered via the Auth service.

The final step is to select when to send the notification. Since we're only testing, we can send it immediately. Clicking on the Send This Push button will send the notification to your selected users.

Schedule push

Conclusion and Next Steps

In this tutorial, you've learned about Ionic Push and how it makes push notifications easier to implement. Through the Ionic dashboard, you were able to customize the notifications that you're sending to users. It also allows you to select which users you want to send the notifications to. 

This works great if you don't have an existing back end already. But if you already have a back end, you might be asking how you can use Ionic Push with your existing web application. Well, the answer for that is the Ionic HTTP API. This allows you to send an HTTP request from your web server to Ionic's server whenever a specific condition is met. The request that you send will then trigger a push notification to be sent to your users. If you want to learn more, you can check out the docs for the Ionic Push Service.

And while you're here, check out some of our other courses and tutorials on Ionic 2!

  • Ionic 2
    Code Your First Ionic 2 App: A Photo Sharing App
    Wernher-Bel Ancheta
  • Ionic
    Get Started With Ionic 2
    Reginald Dawson
  • Ionic
    How to Create a Camera App With Ionic 2
    Ashraff Hathibelagal


How To Build Your Own Action For Google Home Using API.AI


For the holidays, the owner of (and my boss at) thirteen23 gave each employee a Google Home device. If you don’t already know, Google Home is a voice-activated speaker powered by Google Assistant and is a competing product to Amazon’s line of Alexa products.

How To Build Your Own Action For Google Home Using API.AI

I already have the Amazon Echo, and as Director of Technology at thirteen23, I love tinkering with software for new products. For the Amazon Echo, you can create what are called "skills", which allow you to build custom interactions when speaking to the device.

The post How To Build Your Own Action For Google Home Using API.AI appeared first on Smashing Magazine.


Building Your Startup: Approaching Major Feature Enhancements

Final product image What You'll Be Creating

This tutorial is part of the  Building Your Startup With PHP series  on Envato Tuts+. In this series, I'm guiding you through launching a startup from concept to reality using my  Meeting Planner  app as a real-life example. Every step along the way, I'll release the Meeting Planner code as open-source examples you can learn from. I'll also address startup-related business issues as they arise.

How to Approach Major Feature Updates

These days I'm most often working to add small incremental improvements to Meeting Planner. The basics work pretty well, and I'm trying to gradually improve the application based on my vision and people's feedback. Sometimes, my vision is for a bigger change, and that can be harder now that the codebase has grown so much.

In today's tutorial, I'm going to talk about ways to think about making bigger changes to an existing codebase. Specifically, I'll walk you through the impacts of adding the ability for meeting participants to collaboratively brainstorm and decide on activities, i.e. what we should do when we meet up.

If you haven't yet, do try to schedule a meeting, and now you can also schedule an activity. It will help you understand as you go through the tutorial.

Before we begin, please remember to share your comments and feedback below. I monitor them, and you can also reach me on Twitter @lookahead_io. I'm especially interested if you want to suggest new features or topics for future tutorials.

As a reminder, all of the code for Meeting Planner is written in the Yii2 Framework for PHP. If you'd like to learn more about Yii2, check out our parallel series Programming With Yii2.

The Activity Planning Feature

Building Startups - Approaching Major Features - Planning an Activity page

Basically, Meeting Planner and Simple Planner are designed to make scheduling as easy as it can be. You propose a few times and places and share it with one more people for them to weigh in on the options. Then, you decide together, and Meeting Planner keeps track with calendar entries, reminders and simple ways to make adjustments after the fact.

As an example, here's a video of scheduling for groups:

I wanted to expand the scheduling support provided for times and places to the concept of activities. For example, when planning a meetup with your friends, you're essentially asking, should we go to the movies, go dancing or snowboarding.

In other words, I wanted to create a panel like the one for times shown below but for activities: 

Building Startups - Approaching Major Features - The Older Plan a Time Panel

The MVC architecture and my code naming scheme are very similar between meeting times and places, so building activities seemed pretty simple on the surface. However, the decision to do so had wide ramifications.

Scoping the Changes

It's important when adding a big feature to think both about where the code will need to change and also all the places in your application that may be affected to think through the impacts.

Customer-Facing Impacts

From the design side, I thought about how activities will affect the customer-facing service:

  • It will change organizing a meeting to allow a new type, an activity-driven event. There will be an additional panel on the planning page.
  • The activity panel will need to be designed to allow people to choose from defaults or to customize and add their own, e.g. backcountry skiing instead of just skiing, "Go see Star Wars Rogue One" instead of just "Go see a movie."
  • Email invitations will need to include space to list activity options.
  • Calendar events will want to integrate the chosen activity with the subject of the meetup.
  • Organizers may want to send some activity ideas to a friend or a group without having chosen a place, so I need to allow this. Currently, the system doesn't let you send an invitation until there's at least one time and place suggested.
  • If someone requests a change to a meeting, the support for change requests will have to be expanded to support activities.

These were most of the basics. Now, let's think about the code.

Code Impacts

Source Code Branching

Frequently, it's helpful to branch your own code in GitHub so you can work on the new feature apart from the stable production-level codebase. This allows you to return and fix bugs or make smaller incremental changes while working on a major change. The GitHub folks are stricter in a way that makes definite sense for teams:

There's only one rule: anything in the master branch is always deployable.

Since there's just one of me and I'm pretty good at managing my codebase, I am a bit more laissez faire about this rule.

But branching code is also useful for reviewing code changes when you're ready for testing. I'll share a demonstration of this at the end of today's tutorial.

Replicating Common Code

There will be two types of meetings now: those based around only dates and times and those around activities, dates, and times. So the meeting model needs to adapt.

For times and places, there are specifically the models MeetingTime and MeetingPlace as well as models for the preferences for these with participants, called MeetingTimeChoices and MeetingPlaceChoices. You can read more about building this in Building Your Startup With PHP: Scheduling Availability and Choices.

So adding activities would essentially require duplicating these, creating MeetingActivity and MeetingActivityChoices and their accompany controllers, models, views, JavaScript and Ajax and database migrations.

Account and Meeting Settings

Also, organizers have the preference of various account settings and per meeting settings for whether participants can suggest and choose the final activity.

Email Templates

Adding activities also affected email templates for invitations and completed meetings.

Event History and Logs

Since every change to a meeting is logged, each activity option and change also needed to be logged.

Other Miscellaneous Areas

The .ics Calendar file should be changed to include the activity. Ultimately, the API would need to be updated—and even the statistics for the administrative dashboard.

While it seemed simple up front, adding activities actually required a lot of new code and testing.

Coding Highlights

While there's too much new code to cover in one tutorial, let's go through highlighted aspects from some of the concepts above.

Database Migrations

First, I created the database migrations. Earlier I spoke of replicating code with feature aspects in common. Here's an example of the MeetingActivity migration vs. the older MeetingTime table migration:

Here's MeetingTime's migration, and you can see the similarities:

 Ultimately, I needed five for the following new tables:

  1. m161202_020757_create_meeting_activity_table
  2. m161202_021355_create_meeting_activity_choice_table, for storing the availability preferences of each meeting participant for each activity
  3. m161202_024352_extend_meeting_setting_table_for_activities for a particular meeting's settings for adding or choosing activities
  4. m161202_024403_extend_user_setting_table_for_activities for the account's default settings
  5. m161203_010030_extend_meeting_table_for_activities and for adding is_activity to denote the property of a meeting with or without an activity 

Building the MVC Framework for Activities

Building Startups - Approaching Major Features - The Gii Scaffolding Menu

I used Yii's Gii scaffolding capability to create the model, controller, and initial views. I've covered migrations and Gii earlier in the series.

JavaScript and jQuery Changes

There were also substantial additions to the JavaScript and jQuery used, especially now that interacting with planning elements for a meeting is done with Ajax, without refreshing the page.

Here, for example, is the code loop to see if a meeting time is chosen:

By using a common naming scheme, writing the code for activities was replicable from this:

Other functions, such as those that display responses to the user, simply needed to be extended for activities:

Even with a feature so reflective in qualities as existing features, the additions of new code were extensive. Here's more of the JavaScript, for example. This code covers a lot more of the interactive ajax functionality of meeting times on the planning page:

Framework Additions

Certainly, there were models, controllers and views that had to be added. Here's an excerpt from the MeetingActivity.php model which lists a number of default activities the user can quickly typeahead to use:

Building Startups - Approaching Major Features - Activity Typeahead List

And here's an excerpt of the /frontend/views/activity/_form.php with the TypeaheadBasic widget using the above defaultActivityList():

But there are numerous changes to the code outside of common framework needs. Here's the Meeting.php model's canSend(), the function that determines whether the user is allowed to send an invitation for a meeting. It determines if a meeting's met the minimum requirements for sending, such as having a time and activity or time and place.

Below, you can see how a new section had to be added for activities:

Email Templates

Updating the email layouts required a tiny bit of thinking about design and how best to present activities in meeting invitations and confirmations. Here's a sample of the updated email invitation:

Building Startups - Approaching Major Features - Email Invitation Theme

Essentially, if a meeting has an activity, then the invitation includes a wide row above times and places, again replicating a lot of the existing code for times and places:

Reflecting on the Changes

Ultimately, the activity feature required a huge new branch of code. Here's the pull request:

It was so large that I decided to make a fun video scrolling through all the changes in GitHub with appropriate music playing in the background... enjoy:

Overall, building the activities feature was challenging and helpful for me to think about the site architecture and how to make fast, stable progress on the codebase of a one-person startup. Use replication, but reflect first on the overall scope.

The activity feature ended up touching more areas than I had anticipated. 

Planning ahead will help you avoid getting stuck in never-ending coding traps for new features. If you do find yourself in some deep trench, a coding nightmare that just won't end, check in your changes to your feature branch, switch back to master, and work on something else. It helps to clear your head.

I definitely took a slightly different approach to guiding you in this episode, and I hope it was helpful.

Have your own thoughts? Ideas? Feedback? You can always reach me on Twitter @lookahead_io directly. Watch for upcoming tutorials here in the Building Your Startup With PHP series. There's a lot of surprising stuff ahead.

Again, if you haven't tried out Meeting Planner or Simple Planner yet, go ahead and schedule your first meeting:

Related Links

Are There More Developers Than We Think?

Fim de Semana

A cidade estava cheia de romanos (outra vez). Cansada dos cheiros, dos ruídos, dos comércios, dos artifícios... 

César, um boneco (de plástico) sem nobreza.

Melhor ficar em casa a crescer até aos dez anos. Não saí de lá enquanto não li, de fio a pavio, todas as histórias dos dois livros novos (Rubem Alves) que comprei, na semana passada, na livraria do Rui Vaz, no Porto, (Unicepe). A minha infância veio em peso prolongar o meu olhar até pertinho da Alegria. Foi fantástico!


Da adulteza escutei só brinquedos

fabricados com palavras. Achei graça. 

Os adultos são tão pequenos quando

 se põem a "divinar". 

Os seus delírios verbais 



ESR Announces The Open Sourcing Of The World's First Text Adventure
Six TwentyEight Dreamy Blue Pink Vanilla MITT (5.29)
US Senators Propose Bug Bounties For Hacking Homeland Security
Walt Mossberg's Last Column Calls For Privacy and Security Laws

May 28 2017

Leaked 'Standing Rock' Documents Reveal Invasive Counterterrorism Measures
Seven Science Journals Have A Dog On Their Editorial Board
Malicious Apps Brought Ad-Clicking 'Judy' Malware To Millions Of Android Phones
New Privacy Vulnerability In IOT Devices: Traffic Rate Metadata
Silicon Valley Continues To Explore Universal Basic Incomes
Researchers Found Perfect Contraceptives In Traditional Chinese Medicine
Is Amazon's AWS Hiring 'Demolishing The Cult Of Youth'?
Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!