Wednesday, November 20, 2013

Parse.com Push : Advanced targeting based on preferences saved in User object (and not in Installation object)

In Parse.com platform, push notifications are needed to be targeted to Installation objects. While integrating push notification in FootyLight, I needed to use the advanced segment targeting via REST api of parse.com

In the application, the user can follow some teams. Then, when there is an event (i.e. Match starts in few hours, score update during game play) for any of his followed teams, a push notification is sent. We have cron jobs running on servers to trigger push notification sending script. The script uses REST API of parse.

The usual and prescribed way of Parse in this situation is to use the "channel". Every Installation object has a field named "channels" (which is an array). I could save the following team codes in channels (it is called subscribing to channels in Parse terms). Then, sending push to all Installations subscribing to a channel is easy:

curl -X POST \
  -H "X-Parse-Application-Id: XXX" \
  -H "X-Parse-REST-API-Key: YYY" \
  -H "Content-Type: application/json" \
  -d '{
        "channels": [
          "Giants"
        ],
        "data": {
          "alert": "The Giants won against the Mets 2-3."
        }
      }' \
  https://api.parse.com/1/push


But, our case was a bit different. We support multiple devices of a single user. So, if an user has three devices and follows Giants, then he will receive push on all three devices. There will be different Installation objects for each of the devices. And, creating duplicates of subscription (having same data in channels fields of all Installation objects of an user and keeping them synched each time something changed from any of the devices) is not a good idea.

So, what I did is, saved the preferences in a field of the User object and kept a point to owner User object from all Installation objects. Then, I did my R&D and reached a solution in following steps:

Step 1: I tested query for finding Users with "erfan" in their "pushSubscription" API call:
https://api.parse.com/1/classes/_User?limit=0&count=1&where={"pushSubscription":"erfan"}

Response:
{"results":[],"count":1}

The response is as expected, I have only one user with that criteria.

Step 2: I tested query for finding all installations that have user with "erfan" in pushSubscription column API call:

https://api.parse.com/1/classes/_Installation?limit=0&count=1&where={"user":{"$inQuery":{"where":{"pushSubscription":"erfan"},"className":"_User"}}}

Response:
{"results":[],"count":2}

This time also the response is as expected. There are two Installations pointing to that user.

Step 3 (Final step): Now, I tried to send push using the targeting like above

API call (POST) to: https://api.parse.com/1/push with body:
{
  "where":{
     "user":{
       "$inQuery":{
          "where":{"pushSubscription":"erfan"},
          "className":"_User"
       }
     }
   },
"data": {
          "alert": "Test push."
        }
}

Response:
{"result":true}

And it worked like a charm!

Tuesday, January 25, 2011

Adding prefix in subject of FengOffice notification emails

FengOffice sends notifications to users on various occasions. But the issue is, FengOffice does not use any subject prefix for notification. So, it is very hard to differentiate among regular emails and FengOffice notifications in email inbox. And, absence of subject prefix makes it very hard create an appropriate filter rule.

So, a configuration option to add a subject prefix will be very much helpful.
But unfortunately, there is no such configuration option in FengOffice (In the latest version while writing this post, may be this will be added in a future version)

In 'application/models/notifier/Notifier.class.php', I found that there are scope for hooks that can be used in this purpose:

Hook::fire('notifier_email_subject', $subject, $subject);


So, I created a the hook 'notifier_email_subject' to add subject prefix.

I created a new file 'application/hooks/notifier_hooks.php' with the following contents:


<?php

function fengoffice_notifier_email_subject($subject, &$ret) {
$ret = '[FengOffice] ' . $subject;
}

?>



So, [FengOffice] prefix will be added to all notification emails. Because of using hooks, I did not have to modify the core FengOffice code.

The advantage would be, when we update FengOffice version in future, the notifier will not be effected. If I had changed core code, that would be lost because of update.

Friday, November 28, 2008

bm.kkwen66.cn

I dont know how This virus attacked my PC on the first place. But, one day, when Firefox started to crash in regular fashion, I found that, every site I browse from FF, the browser sends some request to "bm.kkwen66.cn". Fishy. Even, when I visit google.com, browser communicates with "bm.kkwen66.cn".

My collegue (who's pc is also infected) reported me that, while visiting gmail, chrome prevented him. Saying that the site "bm.kkwen66.cn" is malicious. So, while accessing gmail, in background, browser was communicating with this site! (and chrome is affected too)

Antiviruses could do nothing. So, what I did is simple. I downloaded ghostwall (a free firewall for windows)

Then, I inquired the ip of bm.kkwen66.cn


Then, in ghostwall, I blocked all traffic to and from this IP.
Thats it! I am done. Though, Firefox still tries to communicate with the site, but the firewall blocks. :)



Why used ghostwall? Its free. And, windows build in firewall does not allow you to block specific IP (atleas in XP SP1)

So, if you are experiencing abnormal behaviour in you Firefox, try browsing several sites and keep eyes on you browser statusbar if it is communicating with malicious sites.

P.S.
- In the first screenshot, you see that while pingin to bm.kkwen66.cn, I get destinaton unreachable. Because, by the time I am writing this entry, I have already blocked that site, so ping packets are blocked

- Don't say that the better solution to this is to switching to linux, virus in linux is rare bla bla bla.

Friday, March 21, 2008

Reading files over NETBeui Protocol

I was trying to copy a file from LAN using NETBeui protocol. The destination workstation had TCP/IP and NETBeui, the source workstation only had NETBeui.

I tried Network.DownloadFile(...) in VB.NET and specified path as
"\\host\path\to\file\filename.ext"

but no luck.

Googled and googled, but found nothing.

Then atlast, I tried FileSystem.CopyFile(...) : guess what, it worked! Gosh!!! I rejected using this method on my first thought and now I found out this is the way to do it.

Ok, if you go through the same problem, simply try this.

Sunday, February 24, 2008

Upgrading phpBB3 RC1 to phpBB3 stable directly

This morning, I upgraded www.DBAnalyst.com (a leading forum on finance in Bangladesh) from phpBB3 RC1 to phpBB stable 3.0.0.

Well, let me explain a bit. In lst march, the forum was deployed using phpBB2. Then, after short time, it was upgraded to phpBB3 Release Candidate 1(RC1). Before phpBB3 stable version was published, there were 7 more release cadidates (upto RC8)

Now, when I was thinking of upgrading to stable version from RC1, then I found that no direct updating package is available from RC1. I had to upgrade by cahin: Rc1->RC2->..->RC8->Stable 3
That was quite unexpected. I didnt find any direct way for this. So, I made out a way.

Here are the steps (assume table prefix for my deployment (RC1) was phpbb_):

1. Deleted all files of previous forum
3. Downloaded and unzipped new version
3. Installed new version as fresh install (using different table prefix, say phpbb3_)
4. Did not delete the install folder
5. manually edited the config.php file, changed the table prefix to the old one (phpbb_)
6. Executed in web browser http://forum_url/install/database_update.php
7. Using phpMyAdmin, deleted all the tables created for the fresh install (phpbb3_), because we will be using the old tables prefixed phpbb_.

This process is safe. You can try this.

Thursday, February 14, 2008

MFC Print Preview problem solved

Today, I was adding Print Preview option in my current project. MFC has made this process very easy. But, the application tended to crash if I invoked the print preview. This was the flow:

>CView::OnFilePrintPreview()
>>AFXAPI _AfxCheckDialogTemplate(LPCTSTR lpszResource, BOOL bInvisibleChild)
>>>HINSTANCE hInst = AfxFindResourceHandle(lpszResource, RT_DIALOG) <<<----- Bingo!! this fails

lpszResource = 0X7803 which is for print preview dialog. But, my app seems
not being able to locate it!

The trace message was :
ERROR: Cannot find dialog template with IDD 30723.

By the by, my app uses MFC in static library. I googled, but found that many
others faced same questions, some found the reson (not being able to locate
the resource), but could not get any solution from the internet.

Then, after much R&D + thinking, I found the solution.

If you face the same problem, just follow these simple steps:

1. Open your project's resource file (*.rc) in text editor

2. find this (including the quotes)
"#include ""afxres.rc"" // Standard components\r\n"

3. Add the following line after that line (including the quotes)
"#include ""afxprint.rc"" // printing/print preview resources\r\n"

4. Find this
#include "afxres.rc" // Standard components

5. Add the following line after that line
#include "afxprint.rc" // printing/print preview resources


----------- You are all done ---------------

Happy programming!

By the way, I am using VS2005

Monday, February 11, 2008

Simple but useful tip for SQL Injection on MySQL

If you are trying SQL Injection on MySQL database, then keep in mind --

In MySQL, the -- ” (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax

Best of luck...